In: Computer Science
package q1; import java.math.BigDecimal; public class Q1 { public static void main(String[] args) { float diameter=7.5f;
} } |
Radius = 3.75
Output: |
public class TestInheritance{
public static void main(String args[]){
Dog d=new Dog();
d.eat();
d.bark();
}
}
class Animal{
void eat() {System.out.print("eating...");}
}
class Dog __________ Animal {
void bark() {System.out.println("barking...");}
void eat() {
super.eat();
System.out.println("only dog food");
}
}
4.1 Fill in the missing keyword in the class Dog declaration to make it a subclass of Animal. (0.5 points)
4.2 In the box provided above, show the output of the program. (2 points)
public double earnings()
{
return getBaseSalary() + Super.earnings();
}
Find and fix the syntax error that is likely to be generated in the above method.
package q1;
import java.util.*;
import java.math.BigDecimal;
public class Main
{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
BigDecimal diameter = sc.nextBigDecimal();//taking
input diameter from user
BigDecimal conv = BigDecimal.valueOf(2);//converting
the value 2 to BigDecimal because radius=diameter/2
BigDecimal radius=diameter.divide(conv);// this will
divide diameter by 2
System.out.println(radius); //print the radius
}
}
==============================================================
4.1
public class TestInheritance{
public static void main(String args[]){
Dog d=new Dog();
d.eat();
d.bark();
}
}
class Animal{
void eat() {System.out.print("eating...");}
}
class Dog extends Animal {
void bark() {System.out.println("barking...");}
void eat() {
super.eat();
System.out.println("only dog food");
}
} 4.2 The output of the
above program is
eating...only dog food
barking...
=================
@Override
public double earnings() {
return getBaseSalary() + super.earnings();
}