Java Exercise for
Class, Inheritance, Interfaces, Packages, Access Modifiers, Constant in Java
Java Exercise for
Class, Inheritance, Interfaces, Packages, Access Modifiers, Constant in Java
Question 1
Interface Bill has a method calculate(float price,int unitsconsumed) .
class Mobile should implement method calculate(float price, int unitsconsumed) and calculate the bill amount of one mobile;
hint: bill amount =unit price * units consumed.
Answer
interface Bill { public float calculate(float price,int unitsconsumed); } public class Mobile implements Bill { public float calculate(float price,int unitsconsumed) //method override { float billAmount; billAmount=price* unitsconsumed; return billAmount; } public static void main(String arg[]) { float tot; Mobile Mon1=new Mobile(); tot=Mon1.calculate(10.2f,250); System.out.println("Your charge=" + tot); } }
Question 2
Interface shape has a constant double type variable A=3.22;
class Circle has a method findArea(int r) to calculate the area of the circle. class circle implements interface shape and used A to calculate area.
hint :area= A*r*r
Answer
interface Shape { final float A=(float)3.22; } public class Circle implements Shape { public float findArea(int r) { float fArea; fArea=A*r*r; return fArea; } public static void main(String arg[]) { float area; Circle Cir1=new Circle(); area=Cir1.findArea(7); System.out.println("Area=" + area); } }
Question 3
Write a java program which has package studentinfo with a class called Student. The class student has a public method showData(String index, String firstname) which receives student index, student name as values and display them.
The application (out of studentinfo) which has class Main with main method imports class Student and call the method showData(String index, String firstname) to display one student information.
Answer
compile below and copy the Student.class into subfolader Student
package studentinfo; public class Student { public void showData(String index, String firstname) { System.out.println(index +"\n"+ firstname); } } import studentinfo.Student; public class Mystudent { public static void main(String arg[]) { Student s1=new Student(); s1.showData("S0001","Peter"); } }
Question 4
Write a java program to add public class called Result which has a public method getResult(int mark) to the previous package studentinfo. This method receives a module mark of a student and display the status as “pass” or “fail”. [Pass mark is 50]
Answer
package studentinfo; public class Result { public void getResult(int mark) { String s1; if (mark <50) s1="Fail"; else s1="Pass"; System.out.println(s1); } } import studentinfo.Result; public class Myresult { public static void main(String arg[]) { Result r1=new Result(); r1.getResult(60); } }
Question 5
Write a java program which has package info with a class called Item. The class Item has a static variable price =10.
The application (out of info package) which has class Bill imports class Item. The method calculateBill(int quantitybought) of the class Bill used value of item price to calculate the bill amount of the item.
Hint: bill amount=price * quantity bought
Use the class Menu with main method to call method calculateBill(int quantitybought).
Answer
package Inventory; public class Item { public static int price=10; } import Inventory.Item; public class Bill1 { public void calculateBill(int quantitybought) { float tot=Item.price*quantitybought; System.out.println("Amount:" + "\t"+ tot); } public static void main(String arg[]) { Bill1 b1=new Bill1(); b1.calculateBill(25); } }
No comments:
Post a Comment