This program is a sample of Java simple I/O, expressions and type conversions. I
ID: 3784712 • 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
package org.students;
import java.text.DecimalFormat;
import java.util.Scanner;
public class DemoProg {
public static void main(String[] args) {
//Declaring variables
int num1,num2,num3,num4;
float f1,f2,f3,f4;
char ch1,ch2,ch3;
//DecimalFormat class object is used to format the decimal point number
DecimalFormat df=new DecimalFormat("#.##");
//Scanner object is used to get the inputs entered by the user
Scanner sc=new Scanner(System.in);
//getting the 4 integers entered by the user
System.out.print("Enter First Integer :");
num1=sc.nextInt();
System.out.print("Enter Second Integer :");
num2=sc.nextInt();
System.out.print("Enter Third Integer :");
num3=sc.nextInt();
System.out.print("Enter fourth Integer :");
num4=sc.nextInt();
//getting the 4 float numbers entered by the user
System.out.print("Enter First Float type number :");
f1=sc.nextFloat();
System.out.print("Enter Second Float type number :");
f2=sc.nextFloat();
System.out.print("Enter Third Float type number :");
f3=sc.nextFloat();
System.out.print("Enter Fourth Float type number :");
f4=sc.nextFloat();
//Getting three characters entered by the user
System.out.print("Enter Three Characters (seperated by space):");
ch1=sc.next(".").charAt(0);
ch2=sc.next(".").charAt(0);
ch3=sc.next(".").charAt(0);
//Displaying the results
System.out.println("The Four Integers are "+num1+" "+num2+" "+num3+" "+num4);
int sum=(num1+num2+num3+num4);
System.out.println("The Sum of Four Integers is "+sum);
int rem=(num1+num3)%(num2+num4);
System.out.println("The remainder of the sum of first and third divided by the sum of the second and fourth :"+rem);
int average=sum/4;
System.out.println("The average of the four integers as an integer is "+average);
float avg=(float)sum/4;
System.out.println("The average of the four integers as an float is "+df.format(average));
System.out.println("The 4 floats in reverse order is :"+f4+" "+f3+" "+f2+" "+f1);
System.out.println("The value of the sum of the middle two floats minus the sum of the first and last floats is :"+((f2+f3)-(f1+f4)));
float fsum=f1+f2+f3+f4;
float favg=fsum/4;
System.out.println("The average of the four floats as a float :"+df.format(favg));
int favgToInt=(int)favg;
System.out.println("The integer portion of the average of the four floats :"+favgToInt);
System.out.println("The fractional portion of the average of the four floats is "+(favg-favgToInt));
float allNosAvg=(sum+fsum)/8;
System.out.println("The average of all the floats and integers as a float :"+df.format(allNosAvg));
System.out.println("The three characters separated by a space :"+ch1+" "+ch2+" "+ch3);
int ach1=(int)ch1,ach2=(int)ch2,ach3=(int)ch3;
System.out.println("The ascii value of each character is :"+ach1+" "+ach2+" "+ach3);
char cach1=(char)(ach1+1),cach2=(char)(ach2+1),cach3=(char)(ach3+1);
System.out.println("The character following each character :"+cach1+" "+cach2+" "+cach3);
char pach1=(char)(ach1-1),pach2=(char)(ach2-1),pach3=(char)(ach3-1);
System.out.println("the character preceeding each character :"+pach1+" "+pach2+" "+pach3);
}
}
________________________
output:
Enter First Integer :23
3Enter Second Integer :4
Enter Third Integer :45
Enter fourth Integer :56
Enter First Float type number :3.4
Enter Second Float type number :4.5
Enter Third Float type number :5.6
Enter Fourth Float type number :6.7
Enter Three Characters (seperated by space):D F G
The Four Integers are 23 34 45 56
The Sum of Four Integers is 158
The remainder of the sum of first and third divided by the sum of the second and fourth :68
The average of the four integers as an integer is 39
The average of the four integers as an float is 39
The 4 floats in reverse order is :6.7 5.6 4.5 3.4
The value of the sum of the middle two floats minus the sum of the first and last floats is :0.0
The average of the four floats as a float :5.05
The integer portion of the average of the four floats :5
The fractional portion of the average of the four floats is 0.05000019
The average of all the floats and integers as a float :22.27
The three characters separated by a space :D F G
The ascii value of each character is :68 70 71
The character following each character :E G H
the character preceeding each character :C E F
_____________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.