0% found this document useful (0 votes)
268 views14 pages

Java Cat

The document describes a Java programming lab assignment submitted by Aditya Sharma. It involves creating classes for vacation planning and summer courses. The assignment includes: 1. Creating a "vacation" class with place, date, activities and costs as data members and get/print methods. Inheriting this to a "SummerVacation" class. 2. Modifying the classes to make the parent an abstract class. 3. Changing it to define an interface for "vacation" and implementing it in "SummerVacation". 4. Creating nested packages - a "person" class in one, extending it in a subpackage, and invoking it from another package.

Uploaded by

Aditya Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
268 views14 pages

Java Cat

The document describes a Java programming lab assignment submitted by Aditya Sharma. It involves creating classes for vacation planning and summer courses. The assignment includes: 1. Creating a "vacation" class with place, date, activities and costs as data members and get/print methods. Inheriting this to a "SummerVacation" class. 2. Modifying the classes to make the parent an abstract class. 3. Changing it to define an interface for "vacation" and implementing it in "SummerVacation". 4. Creating nested packages - a "person" class in one, extending it in a subpackage, and invoking it from another package.

Uploaded by

Aditya Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

JAVA

PROGRAMMING

LAB ASSIGNMENT 2

ADITYA SHARMA

17BCE0165

1. Create a class called vacation

with Data members

(1) Place

(2) Date

(3) Activities [array of elements],

(4) Cost of each activity [array of elements]

Methods

(1) GetMethod

(2) PrintMethod

Inherit the data members and methods to a new class

called “SummerVacation” and include the data

members

(1) CourseTitle

(2) OnlinePlatform [eg: nptel, courseera, udemey..]

(3) Duratio

n Methods

(1) GetMethod

(2) PrintMethod

(3) Calculate the total cost spent in vacation by creating

static Method.
CODE:
package

pkg17BCE0165_AS2;

import java.util.*;

class vacation {

String Place;

String Date;

String[]

Activities; int[]

cost;

void GetMethod(String Place,String Date,String[] Activities,int[]

cost){ this.Place=Place;

this.Date=Date;

this.Activities=Activities

; this.cost=cost;

}

void PrintMethod(){

System.out.println("Place:"+Place

);

System.out.println("Date:"+Date)

System.out.println("Activities:"+Arrays.toString(Activitie

s)); System.out.println("Cost:"+Arrays.toString(cost));

}


}

public class SummerVacation extends

vacation{ String CourseTitle;


String OnlinePlatform;

String Duration;

VoidGetMethod(Strin
g Place,String
Date,String[]
Activities,int[]
cost,String
CourseTitle,String
OnlinePlatform,Strin
g Duration){
super.GetMethod(Place, Date, Activities,

cost); this.CourseTitle=CourseTitle;

this.OnlinePlatform=OnlinePlatform;

this.Duration=Duration;

}

void PrintMethod(){

super.PrintMethod(

);

System.out.println("Course Title: "+CourseTitle);

System.out.println("Online Platform:

"+OnlinePlatform); System.out.println("Duration:

"+Duration);

}
public static void main(String[]

args) { int total=0;

SummerVacation a=new

SummerVacation(); String[]

games={"Swimming","Basketball"}; int[]

price={30,50};

a.GetMethod("Vadodara","1-16-2019",games,price,"Sports","Sports
Academy","10 days");


a.PrintMethod(

); int[]

arr=a.cost;

for(int i=0;i<arr.length;i++)

{

total=total+arr[i];

}

System.out.println("Total Cost: "+total);


}

}

2. Modify the above program by defining the parent class to be


an abstract class.

CODE:
package

pkg17BCE0165_AS2;

import java.util.*;

abstract class vacation {

String Place;

String Date;

String[]

Activities; int[]

cost;

void GetMethod(String Place,String Date,String[] Activities,int[]

cost){ this.Place=Place;

this.Date=Date;

this.Activities=Activities

; this.cost=cost;

void PrintMethod(){
}

}

public class SummerVacation extends vacation {


String CourseTitle;

String OnlinePlatform;

String Duration;

void GetMethod(String Place,String Date,String[] Activities,int[]


cost,String CourseTitle,String OnlinePlatform,String Duration){

super.GetMethod(Place, Date, Activities,

cost); this.CourseTitle=CourseTitle;

this.OnlinePlatform=OnlinePlatform;

this.Duration=Duration;

}

void PrintMethod(){

super.PrintMethod(

);

System.out.println("Course Title: "+CourseTitle);

System.out.println("Online Platform: "+OnlinePlatform);

System.out.println("Duration: "+Duration);

System.out.println("Place: "+Place);

System.out.println("Date: "+Date);

System.out.println("Activities:

"+Arrays.toString(Activities)); System.out.println("Cost:

"+Arrays.toString(cost));

}

public static void main(String[] args) { int total=0;
SummerVacation a=new

SummerVacation(); String[]

games={"Swimming","BasketBall"}; int[]
price={30,50};

a.GetMethod("Vadodara","1-16-2019",games,price,"Sports","Sports
Academy","10 days");

a.PrintMethod(

); int[]

arr=a.cost;

for(int i=0;i<arr.length;i++)

{

total=total+arr[i];

}

System.out.println("Total Cost: "+total);



}

}
3. Make necessary changes in the program by creating an
interface vacation and implement it in a class SummerVacation
as given above.

CODE:
package

pkg17BCE0165_AS2;

import java.util.*;

interface vacation{

abstract void GetMethod(String Place,String Date,String[] Activities,int[]

cost); abstract void PrintMethod();

public class SummerVacation implements

vacation { String Place;

String Date;

String[]
Activities;
int[] cost;

String CourseTitle;

String

OnlinePlatform;

String Duration;

void GetMethod(String Place,String Date,String[] Activities,int[]


cost,String CourseTitle,String OnlinePlatform,String Duration){

this.Place=Place;

this.Date=Date;

this.Activities=Activities;

this.cost=cost;

this.CourseTitle=CourseTitl

e;

this.OnlinePlatform=OnlinePlatfor

m; this.Duration=Duration;

}

public void PrintMethod(){

System.out.println("Place:

"+Place);

System.out.println("Date:

"+Date);

System.out.println("Activities:

"+Arrays.toString(Activities)); System.out.println("Cost:

"+Arrays.toString(cost)); System.out.println("Course Title:

"+CourseTitle); System.out.println("Online Platform:

"+OnlinePlatform); System.out.println("Duration:

"+Duration);

}


public static void main(String[]

args) { int total=0;

SummerVacation a=new SummerVacation();



String[] games={"Swimming","BasketBall"}; int[] price={30,50};
a.GetMethod("Vadodara","1-16-2019",games,price,"Sports","Sports
Academy","10 days");

a.PrintMethod(

); int[]

arr=a.cost;

for(int i=0;i<arr.length;i++)

{

total=total+arr[i];

}

System.out.println("Total Cost: "+total);



}

@Override

public void GetMethod(String Place, String Date, String[] Activities, int[] cost) {

throw new UnsupportedOperationException("Not supported yet."); //To change
body of generated methods, choose Tools | Templates.

}

4. Create a class person with datamembers like name, age, phno with
methods getvalues and printvalues, store the class in a package called
pack1. Define a class named student in subpackage SubPack1 and
extend the class defined as person. Use this class in a program of
different package Pack2, create an object and invoke the methods
defined in pack1 class file.

CODE:

Person.java
package

pak1_17BCE0165_AS2;

import java.util.*;

public class person

{ String

name,phno; int

age;

public void getvalues(){



System.out.println("Enter the name,age, and phone

number."); Scanner sc=new Scanner(System.in);


name=sc.nextLine(

); age=sc.nextInt();

phno=sc.next();

public void printvalues(){

System.out.println("Name:"+name);

System.out.println("Age:"+age);

System.out.println("Phone

number:"+phno);

}
}


Student.java
package

pak1_17BCE0165.SubPak1; import

pak1_17BCE0165.person;

public class student extends

person {

test.java
package Pak2_17BCE0165;

import

pak1_17BCE0165.SubPak1.student;

public class Test {

public static void main(String

args[]){ student s1=new

student(); s1.getvalues();

s1.printvalues();

} }




Ma’am this is to inform you that since I don’t have a java compiler on my mac laptop so
I use an Online Compiler which throws errors while class induction. Ma’am I also have
2 exams tomorrow, I am unable to work out this compilation.

I would be highly obliged if you could help me in this situation, however the codes are
ready and I can show them to you in person also.


Thank you, Ma’am

Yours Sincerely,
ADITYA SHARMA
17BCE0165

You might also like