This program is a sample of Java simple IO expressions and t

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\')

Solution

/**
* The java program SimpleIO that demonstrates the simple
* io operations and prints the corresponding results.
* The prompt prompts user to enter 4 integer, 4 floats
* and 3 characters and do corresponding operations
* and print results to console.
* */
//SimpleIO.java
import java.util.Scanner;
public class SimpleIO
{  
   public static void main(String[] args)
   {
      
       int i1,i2,i3,i4;
       double d1,d2,d3,d4;
       char ch1,ch2,ch3;
       Scanner scan=new Scanner(System.in);
       System.out.println(\"Enter three integer values\");
       i1=scan.nextInt();
       i2=scan.nextInt();
       i3=scan.nextInt();
       i4=scan.nextInt();
      
       System.out.println(\"Enter 4 double values\");
       d1=scan.nextDouble();
       d2=scan.nextDouble();
       d3=scan.nextDouble();
       d4=scan.nextDouble();
      
       System.out.println(\"Enter 3 characters\");
       ch1=scan.next().charAt(0);
       ch2=scan.next().charAt(0);
       ch3=scan.next().charAt(0);
      
       //Line1: the 4 integers separated by one space
       System.out.println();
       System.out.println(i1+\" \"+i2+\" \"+i3+\" \"+i4);
       //Line2:the sum of the integers
       int sum=i1+i2+i3+i4;
       System.out.println(\"Sum: \"+sum);
       //Line3:the remainder of the sum of first and
       //third divided by the sum of the second and fourth
      
       int r=(i1+i3)%(i2+i4);
      
       System.out.println(\"Remainder: \"+r);
       //Line4:the average of the four integers as an integer
       int avg=(i1+i2+i3+i4)/4;
       System.out.println(\"average (in integer): \"+avg);
      
       //Line5:the average of the four integers as a float
       double avgfloat=(i1+i2+i3+i4)/4.0;
       System.out.printf(\"average (in float): %5.2f\ \",avgfloat);
      
       //Line6:the 4 floats in reverse order separated by a space
       System.out.println(d4+\",\"+d3+\",\"+d2+\",\"+d1);
      
       //Line7:the value of the sum of the middle two floats
       //minus the sum of the first and last floats
       System.out.printf(\"Output 7: %5.2f\",(d2+d3)-(d1+d4));
      
       //Line8:the average of the four floats as a float
       double floatavg=(d1+d2+d3+d4)/4.0;
       System.out.printf(\"average (in double): %5.2f\ \",floatavg);
      
       //Line9:the integer portion of the average of the four floats      
       int integerPortion=(int)floatavg;
       System.out.printf(\"Integer portion : %d\ \",integerPortion);
      
       //Line10:the fractional portion of the average of the
       //four floats (do not use the floating point modulus)      
       double floatPortion=floatavg-(int)floatavg;
       System.out.printf(\"Float portion : %5.2f\ \",floatPortion);
  
       //Line11:the average of all the floats and integers as a float
       double a=avg+avgfloat;
       System.out.printf(\"average of integer and floats : %5.2f\ \",a);
      
       //Line12:the three characters separated by a space
       System.out.println(\"Characters separated by space :\"+ch1+\",\"+ch2+\",\"+ch3);
      
       //Line13:the ascii value of each character separated by 1 space      
       System.out.println(\"Ascii values : \"
                           +ch1+\"=\"+(int)ch1
                           +ch2+\"=\"+(int)ch2+
                           +ch1+\"=\"+(int)ch3);
      
       char temp1=ch1;
       char temp2=ch2;
       char temp3=ch3;
      
       //Line14:the character following each character separated by
       //1 space (ie: if the character is an \'b\', the program will print out a \'c\')
      
       temp1=(char) (ch1+1);
       temp2=(char) (ch2+1);
       temp3=(char) (ch3+1);
      
       System.out.println(temp1+\",\"+temp2+\",\"+temp3);
      
      
       //Line15:the character preceeding each character separated by
       //1 space (ie: if the character is an \'b\', then the program will print out a \'a\')
      
       temp1=(char) (ch1-1);
       temp2=(char) (ch2-1);
       temp3=(char) (ch3-1);
      
       System.out.println(temp1+\",\"+temp2+\",\"+temp3);
   }

}


Sample Output:

Enter three integer values
1 2 3 4
Enter 4 double values
1.0 2.0 3.0 4.0
Enter 3 characters
b c d

1 2 3 4
Sum: 10
Remainder: 4
average (in integer): 2
average (in float): 2.50
4.0,3.0,2.0,1.0
Output 7: 0.00average (in double): 2.50
Integer portion : 2
Float portion : 0.50
average of integer and floats : 4.50
Characters separated by space :b,c,d
Ascii values : b=98c=99 d=100
c,d,e
a,b,c

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 descri
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 descri
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 descri
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 descri

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site