Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Part 5: Given the information below, write a Java program to implement this algo

ID: 3878524 • Letter: P

Question

Part 5: Given the information below, write a Java program to implement this algorithm.

Define four integer variables called “a”,”b”,”c”, and “d”.

Set “a” to 2, “b” to 3, c to 5, and d to 7.

Define two double variables called “f” and “g” and initialize to 0.0 .

After these declarations, set the value of f to 2.0 and the value of g to 3.0 .

Define two int variables called intQuotient and remainder.

Define a float variable called quotient .

Write the Java statements needed to find the intQuotient and remainder of b divided by a and save the results into those variables.

Write the Java statements needed to find the quotient of g divided by f and save the results into the variable.

Print the called intQuotient, remainder, and quotient in output statements that explain what the values mean, i.e. don’t just print the numbers. Make sure your output is understandable to someone who doesn’t know the assignment.

Now in your program, ask the user to input new values for a, b, f and g. Get the values from the user, store in the variables, then, using a second set of Java statements, perform the same calculations for intQuotient, remainder, and quotient AND print these outputs.

5.a) Save your working program as Lab1Part5.java . (8 pts)

5.b) What happens in NetBeans if you make the assignment

quotient = b / a;

What value is put in quotient? Is this accurate? Why or why not?         (4 pts)

5.c) Correct the value of quotient by ‘casting’. Your quotient will need to be a decimal number. NOTE: You will need to make one of the integers act like a decimal, i.e. a double, by ‘casting’ the value inside your equation. For example to get a double sum from a and b you would write

sum = a + (double)b;

This would make the right side of the statement give a double type answer.

Show the revised version of the quotient assignment that you would use.(2 pts)

5.d) Also in the file, give one more example of input values that would give invalid values for the quotient. NOTE: This does NOT have to mean that your program is necessarily crashing. A value can be invalid if it is meaningless for that use. As an example, if you are only working with real numbers then you cannot take the square root of -1 because the root is an imaginary number. Therefore, -1 would be an input value that would give a meaningless result so -1 would be an invalid input value. (4 pts)

Put the answers for 5.b - 5.d in your answers file and include a note about 5.a).

Explanation / Answer

5 a)

import java.util.*;

class Lab1Part5
{
public static void main (String[] args)
{
  int a ,b,c,d;
  a= 2;
  b= 3;
  c = 5;
  d = 7;
  
  double f=0.0,g=0.0;
  
  f = 2.0;
  g = 3.0;
  
  
  int intQuotient , remainder;
  float quotient;
  
  intQuotient = b/a;
  remainder = b%a;
  quotient = (float)g/(float)f; //casting from double to float of variable g and f
  
  System.out.println("a = "+a +" b="+b );
  System.out.println("intQuotient = "+intQuotient+" remainder = "+remainder);
  System.out.println("f = "+f +" g = "+g);
  System.out.println("quotient = "+quotient);
  
  
  // input values from user
  
  Scanner sc= new Scanner(System.in);
  System.out.println("Enter the values of a and b : ");
  a= sc.nextInt();
  b= sc.nextInt();

  
  System.out.println("Enter the values of f and g: ");
  f = sc.nextDouble();
  g = sc.nextDouble();
  
  
  
  intQuotient = b/a;
  remainder = b%a;
  quotient = (float)g/(float)f; //casting from double to float of variable g and f
  
  System.out.println("a = "+a +" b="+b );
  System.out.println("intQuotient = "+intQuotient+" remainder = "+remainder);
  System.out.println("f = "+f +" g = "+g);
  System.out.println("quotient = "+quotient);
  
}
}

Output:

a = 2 b=3
intQuotient = 1 remainder = 1
f = 2.0 g = 3.0
quotient = 1.5
Enter the values of a and b :
Enter the values of f and g:
a = 6 b=9
intQuotient = 1 remainder = 3
f = 5.6 g = 3.4
quotient = 0.60714287

5 b) It gives error without casting double values of f and g to float

Main.java:26: error: incompatible types: possible lossy conversion from double to float
quotient = g/f;


5 c)

quotient = (float)g/(float)f; //casting from double to float of variable g and f

5 d)

import java.util.*;

class Division
{
public static void main (String[] args)
{
  int a ,b,c,d;
  a= 2;
  b= 3;
  c = 5;
  d = 7;
  
  double f=0.0,g=0.0;
  
  f = 2.0;
  g = Math.sqrt(-3.0);
  
  
  int intQuotient , remainder;
  float quotient;
  
  intQuotient = b/a;
  remainder = b%a;
  quotient = (float)g/(float)f;
  
  System.out.println("a = "+a +" b="+b );
  System.out.println("intQuotient = "+intQuotient+" remainder = "+remainder);
  System.out.println("f = "+f +" g = "+g);
  System.out.println("quotient = "+quotient);
  
  
  
  
  
  
}
}

Output:

a = 2 b=3
intQuotient = 1 remainder = 1
f = 2.0 g = NaN
quotient = NaN