You are on page 1of 8

Lab 5 (JAVA)

Name-Chandan Kumar
Reg no-18BCE1020
Fac- Prof. Sridevi

1. CODE:
→ Book.java
package java_lab_exercise;

abstract class Book {

String title;

double price;

abstract void setPrice();

→ Fiction.java
package java_lab_exercise;

public class Fiction extends Book{

@Override

void setPrice() {

this.price=24.99;

Fiction(String s){

this.title=s;

//setPrice();

}
→ NonFiction.java
package java_lab_exercise;

public class NonFiction extends Book{

@Override

void setPrice(){

this.price=37.99;

NonFiction(String s){

this.title=s;

→ UseBook.java
package java_lab_exercise;

public class UseBook {

public static void main(String[] args){

Fiction f1=new Fiction("The Great Gatsby");

f1.setPrice();

NonFiction nf1=new NonFiction("A Brief history of time");

nf1.setPrice();

System.out.println("First Fictional Book:");

System.out.println("Name and price-->"+f1.title+" $"+f1.price);

System.out.println("Second Fictional Book:");

System.out.println("Name and price-->"+nf1.title+" $"+nf1.price);

}
OUTPUT:

2. CODE:
package java_lab_exercise;
import java.util.Scanner;
abstract class Themepark{
int n_adult,n_children;
int ticket_cost(){
int x=this.n_adult;
x=x*500;
int y=this.n_children;
y=y*300;
return x+y;
}
abstract void playGame();
}

class Queensland extends Themepark{


Scanner scan=new Scanner(System.in);
boolean[] Games=new boolean[30];

Queensland(int n,int m){


this.n_adult=n;
this.n_children=m;
}
@Override
void playGame(){
int[] cnt=new int[30];
for(int i=0;i<30;i++){
cnt[i]=0;
}
int k;
boolean temp=true;
while(temp){
System.out.println("Enter game number (0-29) press other to exit:");
k=scan.nextInt();
if(k>=0 && k<30){
if(Games[k]==false){
Games[k]=true;
cnt[k]=1;
}
else{
System.out.println("Already played this game!");
cnt[k]++;
}

}
else{
System.out.println("Thank You for playing!");
System.out.println("Stats for number of games not played");
for(int i=0;i<30;i++){
if(cnt[i]==0){
System.out.print(""+i+" ");
}
}
System.out.println("\nGames selected for more than one time:");
for(int i=0;i<30;i++){
if(cnt[i]>1){
System.out.print(""+i+" ");
}
}
System.out.print("\n");
temp=false;
}
}
}
}

class Wonderla extends Themepark{


Scanner scan=new Scanner(System.in);
boolean[] Games=new boolean[30];
Wonderla(int n,int m){
this.n_adult=n;
this.n_children=m;
}

@Override
void playGame(){
int[] cnt=new int[40];
for(int i=0;i<40;i++){
cnt[i]=0;
}
int k;
boolean temp=true;
while(temp){
System.out.println("Enter game number (0-39) press other to exit:");
k=scan.nextInt();
if(k>=0 && k<40){
if(Games[k]==false){
Games[k]=true;
cnt[k]=1;
}
else{
System.out.println("Already played this game!");
cnt[k]++;
}

}
else{
System.out.println("Thank You for playing!");
System.out.println("Stats for number of games not played");
for(int i=0;i<40;i++){
if(cnt[i]==0){
System.out.print(""+i+" ");
}
}
System.out.println("\nGames selected for more than one time:");
for(int i=0;i<40;i++){
if(cnt[i]>1){
System.out.print(""+i+" ");
}
}
System.out.print("\n");
temp=false;
}
}
}
}
public class Lab4_q2 {
public static void main(String[] args){
System.out.println("Queensland");
Queensland q1=new Queensland(10,10);
q1.ticket_cost();
q1.playGame();

System.out.println("Wonderla");
Wonderla w1=new Wonderla(5,5);
w1.ticket_cost();
w1.playGame();
}
}

OUTPUT:
3. CODE
package java_lab_exercise;

interface Shape3D{
double getVolume();
}

class Cuboid implements Shape3D{


private double len,bredth,height;
Cuboid(double l,double b,double h){
this.len=l;
this.bredth=b;
this.height=h;
}
@Override
public double getVolume(){
return ((this.len)*(this.bredth)*(this.height));
}
}

interface Solid3D{
double getDensity();
double getMass();
}

class SolidCuboid extends Cuboid implements Solid3D{


private double density;
SolidCuboid(double l,double b,double h,double d){
super(l,b,h);
this.density=d;
}
SolidCuboid(double l,double b,double h){
super(l,b,h);
this.density=1.0;
}
@Override
public double getDensity() {
return this.density;
}

@Override
public double getMass() {
return(this.getVolume()*this.density);
}
}

public class Lab4_q3 {


public static void main(String[] args){
System.out.println("Implementation of cuboid class:");
Cuboid c1=new Cuboid(10,5,2);
double V1=c1.getVolume();
System.out.println("Volume of Cuboid="+V1);

System.out.println("\nImplementation of SolidCuboid class with den=1:");


SolidCuboid s1=new SolidCuboid(10,5,4);
double V2=s1.getVolume();
double D1=s1.getDensity();
double M1=s1.getMass();
System.out.println("Volume of Cuboid="+V2);
System.out.println("Density of Cuboid="+D1);
System.out.println("Mass of Cuboid="+M1);

System.out.println("Implementation of SolidCuboid class:");


SolidCuboid s2=new SolidCuboid(10,5,4,1.2);
double V3=s2.getVolume();
double D2=s2.getDensity();
double M2=s2.getMass();
System.out.println("Volume of Cuboid="+V3);
System.out.println("Density of Cuboid="+D2);
System.out.println("Mass of Cuboid="+M2);
}
}

OUTPUT:

You might also like