Professional Documents
Culture Documents
Java 6
Java 6
Rg.no:21MIS0019
Java assignment 6
Code:
import java.util.Scanner;
class requirements{
int no_sold_temper_glass,no_sold_cover,no_sold_earphones,no_sold_headset;
int no_pur_temper_glass,no_pur_cover,no_pur_earphones,no_pur_headset;
float sold_pri_temper_glass,sold_pri_cover,sold_pri_earphones,sold_pri_headset;
float pri_pur_temper_glass,pri_pur_cover,pri_pur_earphones,pri_pur_headset;
float tot_pro_temper_glass,tot_pro_cover,tot_pro_earphones,tot_pro_headset;
float tot_sold_temper_glass,tot_sold_cover,tot_sold_earphones,tot_sold_headset;
float tot_sold,tot_pur,tot_f;
void get_sold() {
super.sold_pri_temper_glass=sc.nextFloat();
super.no_sold_cover=sc.nextInt();
super.sold_pri_cover=sc.nextFloat();
super.no_sold_earphones=sc.nextInt();
super.sold_pri_earphones=sc.nextFloat();
super.no_sold_headset=sc.nextInt();
super.sold_pri_headset=sc.nextFloat();
void get_product() {
super.no_pur_temper_glass=sc.nextInt();
super.pri_pur_temper_glass=sc.nextFloat();
super.no_pur_cover=sc.nextInt();
super.pri_pur_cover=sc.nextFloat();
super.no_pur_earphones=sc.nextInt();
super.pri_pur_earphones=sc.nextFloat();
super.no_pur_headset=sc.nextInt();
System.out.print("Enter The Price Per purchased headset : ");
super.pri_pur_headset=sc.nextFloat();
void cal_product() {
tot_pro_temper_glass=no_pur_temper_glass*pri_pur_temper_glass;
tot_pro_earphones=no_pur_earphones*pri_pur_earphones;
tot_pro_cover=no_pur_cover*pri_pur_cover;
tot_pro_headset=no_pur_headset*pri_pur_headset;
tot_pur=tot_pro_temper_glass+tot_pro_earphones+tot_pro_cover+tot_pro_headset;
void cal_sold() {
tot_sold_temper_glass=no_sold_temper_glass*sold_pri_temper_glass;
tot_sold_earphones=no_sold_earphones*sold_pri_earphones;
tot_sold_cover=no_sold_cover*sold_pri_cover;
tot_sold_headset=no_sold_headset*sold_pri_headset;
tot_sold=tot_sold_temper_glass+tot_sold_earphones+tot_sold_cover+tot_sold_headset;
void profit_or_loss() {
super.tot_f=super.tot_sold-super.tot_pur;
if(super.tot_f>0) {
else {
}
class Main {
String op;
while(true) {
op=sc.next();
switch(op.toLowerCase()) {
case "sold":
d.get_sold();
d.cal_sold();
break;
case "purchased":
d.get_product();
d.cal_product();
break;
case "display":
d.profit_or_loss();
break;
case "exit":
System.exit(0);
break;
default:
}
Output:
2. Write the java program for different shapes (Line, Triangle, Rectangle
and
Circle) and compute the area of shapes. Apply the multilevel inheritance
concept
in the above code to show how method overriding takes place and how the
final
keyword is used for avoiding method overriding?
Code:
class Line{
int length;
Line(){
length = 5;
}
void area(){
System.out.println("Line Has No Area");
}
}
class Triangle extends Line{
int height,base;
Triangle(){
height = 6;
base = 3;
}
void area(){
System.out.println("Area of Triangle = "+0.5*height*base);
}
}
class Rectangle extends Triangle{
int length,breadth;
Rectangle(){
length = 3;
breadth = 5;
}
void area(){
System.out.println("Area of Rectangle = "+length*breadth);
}
}
class Circle extends Rectangle{
int radius;
Circle(){
radius = 2;
}
void area(){
System.out.println("Area of Circle = "+3.14*radius*radius);
}
}
public class Week {
public static void main(String[] args) {
Line l = new Line();
l.area();
Triangle t = new Triangle();
t.area();
Rectangle r = new Rectangle();
r.area();
Circle c = new Circle();
c.area();
}
}
Output: