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

Write the output of the following segment of code and explain your results. int

ID: 3575717 • Letter: W

Question


Write the output of the following segment of code and explain your results. 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); Write the output of the following program and explain your results. class StudentlnMyClass { 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 getNumberOfStudentsInMyClass() { return(numberOfStudents); }

Explanation / Answer

Please follow the code and comments for desciption :

CODE :

public class Test { // class

    public static void main(String[] args) { // driver method
        int x = 7, y = 4, z; // local variables
        float z1, z2, z3;
        z1 = x / y; // result
        z2 = (float) (x / y); // type cast the result
        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); // print the formatted data
    }
}

OUTPUT :

z = 1   z1 = 1.00 z2 = 1.00 z3 = 1.75

EXPLANATION :

Here as per the given data the normal division of the numbers do result as 1.75. Now moving on to the process step by step we find that the z1 is the result data after the division of variables that are of integre type and that the data to be saved as a float type. So the data which is of flot type needs to be typecasted and that the value before the decimal point is only saved resulting the data as 1.00.

Now for the z2 the values are first divide and that they are type casted explicitly which the more or less as the above case. So this do results the same result.

For z3 the data is typecasted and that is divided so the answer here best fits and the result is obtained as 1.75.

For the case of z the answer needs to be save as an integer so the answer is 1.


Hope this is helpful.