This program is a sample of Java simple I/O, expressions and type conversions. I
ID: 667350 • Letter: T
Question
This program is a sample of Java simple I/O, expressions and type conversions. It will expect as input the three lines of input and produce the output as described below.
Format for output: All floating point output is formatted such that there are two places to the right of the decimal point.
This program expects as input: line 1: 4 integers line 2: 4 floats (double) line 3: 3 characters separated by spaces It will produce the output: line 1: the 4 integers separated by one space line 2: the sum of the integers line 3: the remainder of the sum of first and third divided by the sum of the second and fourth line 4: the average of the four integers as an integer line 5: the average of the four integers as a float line 6: the 4 floats in reverse order separated by a space line 7: the value of the sum of the middle two floats minus the sum of the first and last floats line 8: the average of the four floats as a float line 9: the integer portion of the average of the four floats line 10: the fractional portion of the average of the four floats (do not use the floating point modulus) line 11: the average of all the floats and integers as a float line 12: the three characters separated by a space line 13: the ascii value of each character separated by 1 space line 14: the character following each character separated by 1 space (ie: if the character is an 'b', the program will print out a 'c') line 15: the character preceeding each character separated by 1 space (ie: if the character is an 'b', then the program will print out a 'a')Explanation / Answer
import java.io.*;
public class sampleio {
public static void main(String args[]) throws IOException{
System.out.println("Enter the four integers: ");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int x1 = Integer.parseInt(br.readLine());
int x2 = Integer.parseInt(br.readLine());
int x3 = Integer.parseInt(br.readLine());
int x4 = Integer.parseInt(br.readLine());
System.out.println("Enter the four floating numbers: ");
double y1 = Double.parseDouble(br.readLine());
double y2 = Double.parseDouble(br.readLine());
double y3 = Double.parseDouble(br.readLine());
double y4 = Double.parseDouble(br.readLine());
System.out.println("Enter the 3 characters: ");
char d1 = (char) br.read();
char d2 = (char) br.read();
char d3 = (char) br.read();
int sum =x1+x2+x3+x4;
System.out.println("Entered integers are: "+x1+" "+x2+" "+x3+" "+x4);
System.out.println("Sum of the four integers is: "+sum);
int a=(x1+x3)/(x2+x4);
System.out.println("The result of (1+3)/(2+4): "+a);
double avg1=(x1+x2+x3+x4)/4;
int avg2=(int) avg1;
System.out.println("Average of integers in integer format is: "+avg2);
System.out.println("Average of integers in floating format is: "+avg1);
double avg3=(y1+y2+y3+y4)/4;
System.out.println("Average of floating nos. is: "+avg3);
int avg4=(int) avg3;
System.out.println("Integer portion of the average of floating nos. is: "+avg4);
double g2 = avg3 - (int) avg3;
System.out.println("Floating portion of the average of floating nos. is: "+g2);
System.out.println("Entered floating point nos. in reverse are: "+y4+" "+y3+" "+y2+" "+y1);
double b=(y2+y3)-(y1+y4);
System.out.println("The result of (2+3)-(1+4) is: "+b);
double avg5=(x1+x2+x3+x4+y1+y2+y3+y4)/8;
System.out.println("Average of all integers and floats in floating format is: "+avg5);
System.out.println("Entered characters are: "+d1+" "+d2+" "+d3);
int asci1 = (int) d1;
int asci2 = (int) d2;
int asci3 = (int) d3;
System.out.println("ASCII value of all characters are: "+asci1+" "+asci2+" "+asci3);
d1 = (char)(d1+1);
d2 = (char)(d2+1);
d3 = (char)(d3+1);
System.out.println("Following characters are: "+d1+" "+d2+" "+d3);
d1 = (char)(d1-2);
d2 = (char)(d2-2);
d3 = (char)(d3-2);
System.out.println("Preceeding characters are: "+d1+" "+d2+" "+d3);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.