AP Computer Science- Inheritance Worksheet 2 DIRECTIONS: Fill in each blank with
ID: 3882351 • Letter: A
Question
AP Computer Science- Inheritance Worksheet 2 DIRECTIONS: Fill in each blank with the correct answer. Assume each statement happens in that one statement may affect the next statement. Some sections might print more than once. Write ERR for any segments that cause errors. (Refer to Moodle to WS1) class G private int x public GOIX 3:1 public void setx(int val x vali public String tostring) return +x class H extends G private int y: public Hoy: public oid seer tint val){ y-vali return y "+super.toString) IILIAIAIAIIIAINM //test code in the main method G one new GO; out.printin(one) one.setX(5) out.printin(one) H two- new H) two.setx (-2) two.setY(11) out.printin(two) G three new H0 out printinithree) three.setx(8) three.setY (21)1 out.printinfthree): G four -new ) four.setX (11): out.println (four) four.setx(6) t (H) four) setY (i4 out.printin(four) 3. H five new Rt) five.setY (7) out.printin(Eive); ive.setx(16) five.setY (9) out.printin(five): C A+ Computer Science-Worksheet- www.apluscompsci.comExplanation / Answer
Outputs
1.
G G();
System.out.println(one);
one.setX(5);
System.out.println(one);
H two=new H();
two.setX(-2);
two.setY(11);
System.out.println(two); //1
we get 11 -2 as output here.
Explanation: two is the instance of H class. when we print it .it will call toString() of H class and print value of y and x.
-----------------------------------------------------------------------------------------------------------------------------------------------------
2.
G three =new H();
System.out.println(three);
three.setX(8);
three.setY(21);
System.out.println(three); //2
Output: ERROR
Because we cant call setY() method of H class using object of G class. so compiler give error at three.setY(21); line
----------------------------------------------------------------------------------------------------------------------------------------------------
3.
G four=new H();
four.setX(11);
System.out.println(four);
four.setX(6);
((H)four).setY(14);
System.out.println(four); //3
Output: 14 6
Explaination: value of x is set to 6 and value of y is set to 14. so when we print four it will call toString() of H class and print output 14 and 6.
-------------------------------------------------------------------------------------------------------------------------------------------------------
4.
H five=new H();
five.setY(7);
System.out.println(five);
five.setX(16);
five.setY(9);
System.out.println(five); //4
Output: 9 16
Explaination: value of x is set to 16 and value of y is set to 9. so when we print four it will call toString() of H class and print output 9 and 16.
--------------------------------------------------------------------------------------------------------------------------------------------------
Full java code to run and check
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package code_test;
class G
{
private int x;
public G() {
x=3;
}
public void setX(int val) {
x = val;
}
public String toString()
{
return ""+x;
}
}
class H extends G
{
private int y;
public H() {
y=4;
}
public void setY(int val) {
y = val;
}
public String toString()
{
return ""+y+" "+super.toString();
}
}
/**
*
* @author Kamal
*/
public class Code_test {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
G G();
System.out.println(one);
one.setX(5);
System.out.println(one);
H two=new H();
two.setX(-2);
two.setY(11);
System.out.println(two);
G three =new H();
System.out.println(three);
three.setX(8);
//three.setY(21);
//System.out.println(three);
G four=new H();
four.setX(11);
System.out.println(four);
four.setX(6);
((H)four).setY(14);
System.out.println(four);
H five=new H();
five.setY(7);
System.out.println(five);
five.setX(16);
five.setY(9);
System.out.println(five);
}
}