I need to provide the ouput of this program for my homework. There might semicol
ID: 3812933 • Letter: I
Question
I need to provide the ouput of this program for my homework. There might semicolins missing and syntax errors since I typed of myself looking at a piece of paper. Dont worry about the syntax errors. I want to learn what each step does, and what we will ouput from the program.
---------------------------------------------------------------------------------------------------------------------------------------
public class testCase {
public static void main(String[] args){
PT tester = new PT();
int a1 = 13;
Fraction a2 = new Fraction(1,2);
Fraction a3 = new Fraction(1,4);
System.out.println("Print 1 a1=" +a1+ "a2 =" +a2+ "a3 =" +a3);
tester.cV(a1,a2,a3);
System.out.println("Print 4 a1=" +a1+ "a2 =" +a2+ "a3 =" +a3)
}
}
public class PT {
public void cV (int v1, Fraction v2, Fraction v3){
System.out.println("On entry");
System.out.println("Print 2 v1=" + v1 + "v2 = " + v2 + "v3 = " + v3);
v1 = 5;
v2.sD(10);
v3 = new Fraction (3,4);
System.out.println("On exit:");
System.out.println("Print 3 v1=" + v1 + "v2 = " + v2 + "v3 = " + v3);
}
}
public class fraction{
private int denominator;
private int numerator;
public Fraction (int num, int denom){
numerator = denom
}
public void sD (int value){
denominator = value;
}
/**
to String returns the string value of fraction
and is automatically invoked when needing a string value
for an instance of type Num
*/
public String toString(){
retuen numerator + "/" + denominator;
}
}
Explanation / Answer
Program:-
public class testCase {
public static void main(String[] args){
PT tester = new PT();
int a1 = 13;
Fraction a2 = new Fraction(1,2);
Fraction a3 = new Fraction(1,4);
System.out.println("Print 1 a1=" +a1+ "a2 =" +a2+ "a3 =" +a3);
tester.cV(a1,a2,a3);
System.out.println("Print 4 a1=" +a1+ "a2 =" +a2+ "a3 =" +a3);
}
}
class PT {
public void cV (int v1, Fraction v2, Fraction v3){
System.out.println("On entry");
System.out.println("Print 2 v1=" + v1 + "v2 = " + v2 + "v3 = " + v3);
v1 = 5;
v2.sD(10);
v3 = new Fraction (3,4);
System.out.println("On exit:");
System.out.println("Print 3 v1=" + v1 + "v2 = " + v2 + "v3 = " + v3);
}
}
class Fraction{
private int denominator;
private int numerator;
public Fraction (int num, int denom){
numerator = denom;
}
public void sD (int value){
denominator = value;
}
/**
to String returns the string value of fraction
and is automatically invoked when needing a string value
for an instance of type Num
*/
public String toString(){
return numerator + "/" + denominator;
}
}
i fixed all the issues which may you faced.
Now i explained this program Execution.
The program execution start from public static void main(String[] args){
in testCase class.
PT tester = new PT(); // This line creates a object of PT class by calling no-argument constructor.
int a1 = 13; // 13 is assign to variable a1
Fraction a2 = new Fraction(1,2); // This line creates a object of Fraction class by calling two-argument constructor.
Now program flows goes to two-argument constructor. i.e.
public Fraction (int num, int denom){
numerator = denom;
}
// This constructor assign value of denom to Fraction class instance variable numerator under reference variable a2.
Similarly Fraction a3 = new Fraction(1,4); works
// It assign value of denom to Fraction class instance variable numerator under reference variable a3.
System.out.println("Print 1 a1=" +a1+ "a2 =" +a2+ "a3 =" +a3);
// This line prints value of a1 is 13
//when we want to print reference variable then it call override toString method.
hence to print a2 it call
public String toString(){
return numerator + "/" + denominator;
}
This toString method return "2/0"
So a2 print 2/0
similarly a3 print 4/0.
After that it execute line tester.cV(a1,a2,a3);
it call cV method of PT class.
public void cV (int v1, Fraction v2, Fraction v3){
System.out.println("On entry");
System.out.println("Print 2 v1=" + v1 + "v2 = " + v2 + "v3 = " + v3);
v1 = 5;
v2.sD(10);
v3 = new Fraction (3,4);
System.out.println("On exit:");
System.out.println("Print 3 v1=" + v1 + "v2 = " + v2 + "v3 = " + v3);
}
this method print firstly "On entry"
after that it execute System.out.println("Print 2 v1=" + v1 + "v2 = " + v2 + "v3 = " + v3);
Here value of v1 is 13. as a1 is assign to v1.
value of v2 is 2/0 as a2 is assign to v2.
value of v3 is 4/0 as a3 is assign to v3.
so it print like Print 2 v1=13v2 = 2/0v3 = 4/0
after thhat v1 is changed to 5.
v2.sD(10); // This line call sD method under reference variable of v2
public void sD (int value){
denominator = value;
}
in sD method, value is assigned to instance variable denominator.
Again we call two argument constructor.
v3 = new Fraction (3,4);
again 4 is assign to instance variable numerator under reference variable v3.
System.out.println("On exit:");
System.out.println("Print 3 v1=" + v1 + "v2 = " + v2 + "v3 = " + v3);
here, value of v1 is 5.
v2 will print 2/10. because previous we call sD method on reference variable v2. so 10 assign to denominator.
v3 will print 4/0. because value of denominator is assign on reference variable v2 not on v3.
So it print
On exit:
Print 3 v1=5v2 = 2/10v3 = 4/0
now, program execution goes to System.out.println("Print 4 a1=" +a1+ "a2 =" +a2+ "a3 =" +a3)
it print Print 4 a1=13a2 =2/10a3 =4/0
Ouput of program:-
Print 1 a1=13a2 =2/0a3 =4/0
On entry
Print 2 v1=13v2 = 2/0v3 = 4/0
On exit:
Print 3 v1=5v2 = 2/10v3 = 4/0
Print 4 a1=13a2 =2/10a3 =4/0