write the output of the following segment of code and explain your results (0pts
ID: 3575720 • Letter: W
Question
write the output of the following segment of code and explain your results (0pts) int x 7, y 4, z; float zi,z2,z3; z2 (float)(x/y); zy (float)x/y; zmxly; z3 %5.2f In", z, zi, z2, zy); System.out printf z %d zn %5.2f zz 965.2 10. write the output of the following program and explain your results. (0pts) class String name; int age; double ll transcripts; double gpa; static int numberofStudents; public name, int age, double 0 transcripts) this name name this age age; this transcriptsmtranscripts; numberOfStudents++; public int getNumberofstudentsInMyClass0 return numberoStudents) public double getGPA0 double sum mo for(int i-o; iExplanation / Answer
1) public class Test{
public static void main(String args[]){
int x= 7,y=4,z;
float z1,z2,z3;
z1=x/y;
z2=(float)(x/y);
z3=(float)x/y;
z=x/y;
System.out.printf("z=%d z1=%5.2f z2=%5.2f z3=%5.2f ",z,z1,z2,z3);
//Output: z=1 z1= 1.00 z2= 1.00 z3= 1.75
}
}
Output:
z=1 z1= 1.00 z2= 1.00 z3= 1.75
Explanation :-
float data type format % a.bf where 'a' denote total width and 'b' number of decimal spaces.
z is interger so z=x/y output is integer ie. 1
z1 is float so z1=x/y output is float ie. 1.00
z2 is float so z2=(float)(x/y) is float and output of (x/y) wil converto float data type ie. 1.00
z3 is float so z3=(float)x/y is float and x/y wil converto float data type ie. 1.75
==================================================================
2) class StudentInMyClass{
String name;
int age;
double [] transcripts;
double gpa;
static int numberOfStudents;
public StudentInMyClass(String name, int age, double[] transcripts)
{
this.name = name;
this.age = age;
this.transcripts = transcripts;
numberOfStudents ++;
}
public int getNumberOfStudentInMyClass()
{
return(numberOfStudents);
}
public double getGPA()
{
double sum = 0;
System.out.printf("Sum befor calculation: %5.2f ",sum);
for(int i=0;i<transcripts.length;i++)
sum += transcripts[i];
System.out.printf("Sum after calcualtion: %5.2f ",sum);
for(int i=0;i<transcripts.length;i++)
gpa = sum / transcripts.length;
System.out.printf("gpa%5.2f ",gpa);
return gpa;
}
}
public class TestVariables
{
public static void main(String[] args ){
double [] trans1 ={3.0,3.5,3.9};
StudentInMyClass john = new StudentInMyClass("John",20,trans1);
System.out.printf("John: my GPA is %5.2f, the number of student in my class is %d ", john.getGPA(),john.getNumberOfStudentInMyClass());
double [] trans2 = {3.1,2.9,3.4};
StudentInMyClass kerry = new StudentInMyClass("Kerry",21,trans2);
System.out.printf("Kerry: my GPA is %5.2f, the number of student in my class is %d ", kerry.getGPA(),john.getNumberOfStudentInMyClass());
System.out.printf("John: my GPA is %5.2f, the number of student in my class is %d ", john.getGPA(),john.getNumberOfStudentInMyClass());
}
}
Output:-
Sum befor calculation: 0.00
Sum after calcualtion: 10.40
gpa 3.47
John: my GPA is 3.47, the number of student in my class is 1
Sum befor calculation: 0.00
Sum after calcualtion: 9.40
gpa 3.13
Kerry: my GPA is 3.13, the number of student in my class is 2
Sum befor calculation: 0.00
Sum after calcualtion: 10.40
gpa 3.47
John: my GPA is 3.47, the number of student in my class is 2
Explanation :-
float data type format % a.bf where 'a' denote total width and 'b' number of decimal spaces.
Here numberOfStudents is static variable the initialization has been perform once at a time of memory allocation by compiler.
numberOfStudents variable is retain its value during execution of program each time function is called sum incremented by next integer.
Here every time sum is initialized to 0 and it get calculated by for loop till the length of transcripts using this sum gpa is caculated.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.