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.

** please write using a tokenizer**

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

GettingInputs.java

import java.text.DecimalFormat;
import java.util.Scanner;
import java.util.StringTokenizer;

public class GettingInputs {

   public static void main(String[] args) {
       //Declaring variables
       int num1,num2,num3,num4,i=0;
       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 Four Integers (Seperated by space):\");
String str=sc.nextLine();
  
int intNos[]=new int[4];
StringTokenizer st = new StringTokenizer(str,\" \");
while(st.hasMoreTokens())
{
   intNos[i]=Integer.parseInt(st.nextToken());
   i++;
}
num1=intNos[0];
num2=intNos[1];
num3=intNos[2];
num4=intNos[3];
  

  
//getting the 4 float numbers entered by the user
System.out.print(\"Enter Four Float type numbers (Seperated by space) :\");
String str1=sc.nextLine();
  
float flotNos[]=new float[4];
StringTokenizer st1 = new StringTokenizer(str1,\" \");
i=0;
while(st1.hasMoreTokens())
{
   flotNos[i]=Float.parseFloat(st1.nextToken());
   i++;
}
f1=flotNos[0];
f2=flotNos[1];
f3=flotNos[2];
f4=flotNos[3];
  
char charr[]=new char[3];
  
//Getting three characters entered by the user
System.out.print(\"Enter Three Characters (seperated by space):\");

for(i=0;i<3;i++)
{
   charr[i]=sc.next(\".\").charAt(0);
}
ch1=charr[0];
ch2=charr[1];
ch3=charr[2];
  
//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 Four Integers (Seperated by space):23 34 45 56
Enter Four Float type numbers (Seperated by space) :3.4 4.5 5.6 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

_______________________________

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