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

What is the output of the following program? Give the exact format of the output

ID: 3852822 • Letter: W

Question

What is the output of the following program? Give the exact format of the output. public class tests1 { public static void main (String [] args) { int d1 = 31: int d5 = %2: d1/= 2: int d4 = %2: d1/= 2: int d3 = %2: d1/= 2: int d2 = %2: d1/= 2: System.out.println ("Answer: " + d1 + d2 + d3 + d4 + d5): } } Convert the following function consisting of a casecaded if statement to a function consisting of a switch statement: private static char grade (int score) { if (score > 89 && score 79 && score 69 && score 59 && score 0 && score

Explanation / Answer

(1)

public class tests1
{
public static void main(String args[])
{
int d1=31;//d1 is 31
int d5=d1%2;//so 31%2 remmainder is 1 so d5 is 1
d1/=2;//or we can write d1=d1/2=31/2 that 15.5 but it's integer so d1 becomes 15 instead of 15.5
int d4=d1%2;//so d1%2=15%2 so remainder is 1 do d4 is 1
d1/=2;//again we can write d1=d1/2=15/2 that 7.5 but it's integer so d1 becomes 7 instead of 7.5
int d3=d1%2;//so 7%2 remmainder is 1 so d3 is 1
d1/=2;//or we can write d1=d1/2=7/2 that 3.5 but it's integer so d1 becomes 3 instead of 3.5

int d2=d1%2;//so d1%2=3%2 so remainder is 1 do d2 is 1
d1/=2;//again we can write d1=d1/2=3/2 that 1.5 but it's integer so d1 becomes 1 instead of 7.5
so all of the values are 1,1,1,1,1 so output is also 11111
System.out.println("Answer "+d1+d2+d3+d4+d5);
  
}
}

(2)

import java.io.*;
import java.util.*;
public class tests1
{
public static void main(String args[])
{
int score;
System.out.println("Enter Score ");
Scanner sc=new Scanner(System.in);
score=sc.nextInt();
System.out.println(grade(score));
}
public static char grade(int score)
{
int ch=score/10;
switch(ch)
{
case 10 :
case 9 : return 'A';
case 8 : return 'B';
case 7 : return 'C';
case 6 : return 'D';
case 5 :
case 4 :
case 3 :
case 2 :
case 1 :
case 0 :return 'F';
default : System.out.println("Not a Grade ");
System.exit(0);//it exits here
return 'N';//we need to return a character to remove syntax error
}
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote