#
Instance Variables and Methods
⚡ [NOTE!] ⚡
You need to write only the code for the design class. You cannot change any given code from the tester/driver code.
#
Problem-1
Instance Variable
easy
Below you can find the StudentTester.java code. Your task is to design the Student class, so that the required output is shown.
public class StudentTester{
public static void main(String[] args){
Student s1 = new Student();
Student s2 = new Student();
System.out.println("1==========");
System.out.println("Name: "+s1.name);
System.out.println("ID: "+s1.id);
System.out.println("Department: "+s1.department);
System.out.println("2==========");
System.out.println("Name: "+s2.name);
System.out.println("ID: "+s2.id);
System.out.println("Department: "+s2.department);
System.out.println("3==========");
s1.name = "Messi";
s1.id = 12345678;
s1.department = "BBS";
System.out.println("4==========");
System.out.println("Name: "+s1.name);
System.out.println("ID: "+s1.id);
System.out.println("Department: "+s1.department);
System.out.println("5==========");
s2.name = "Ronaldo";
s2.id = 87654321;
s2.department = "CSE";
System.out.println("6==========");
System.out.println("Name: "+s2.name);
System.out.println("ID: "+s2.id);
System.out.println("Department: "+s2.department);
}
}
1==========
Name: No Name
ID: 0
Department: MNS
2==========
Name: No Name
ID: 0
Department: MNS
3==========
4==========
Name: Messi
ID: 12345678
Department: BBS
5==========
6==========
Name: Ronaldo
ID: 87654321
Department: CSE
#
Problem-2
Instance Variable and Method
easy
NOTE:This question is little bit modified version of the previous question.
Below you can find the StudentTester.java code. Your task is to design the Student class, so that the required output is shown.
public class StudentTester{
public static void main(String[] args){
Student s1 = new Student();
Student s2 = new Student();
System.out.println("1==========");
s1.createStudent("Messi", 12345678, "BBS");
System.out.println("2==========");
s2.createStudent("Ronaldo", 87654321, "CSE");
System.out.println("3==========");
s1.displayInfo();
System.out.println("4==========");
s2.displayInfo();
}
}
1==========
Student created successfully
2==========
Student created successfully
3==========
Name: Messi
ID: 12345678
Department: BBS
4==========
Name: Ronaldo
ID: 87654321
Department: CSE
#
Problem-3
Instance Variable and Method
easy
You are a huge anime fan and an excellent programmer. However, due to excessive pressure from your university you cannot remember which animes you have watched. Therefore you want to develop something that will keep track of what animes you want to watch in near future, so that when you are free you can check your wish list and binge watch them. Your task is to design WishList class so that the required output is shown.
⚡ [NOTE!] ⚡
You can add only 3 anime to your wishlist.
public class WishListTester {
public static void main(String[] args) {
WishList w1 = new WishList();
WishList w2 = new WishList();
System.out.println("1=======");
w1.createWishList("Harry");
System.out.println("2=======");
w1.showDetails();
System.out.println("3=======");
w1.addToWishList("Blue Lock");
w1.addToWishList("Death Note");
w1.addToWishList("Dragon Ball Daima");
w1.addToWishList("Attack on Titan");
System.out.println("4=======");
w1.showDetails();
System.out.println("5=======");
w2.createWishList("Jack");
System.out.println("6=======");
w2.addToWishList("Jujutsu Kaisen");
w2.addToWishList("Fullmetal Alchemist");
System.out.println("7=======");
w2.showDetails();
}
}
1=======
WishList for Harry created succesfully
2=======
Wish List Name: Harry
Anime List:
No anime in Wish List
3=======
Blue Lock anime added to the wishlist
Death Note anime added to the wishlist
Dragon Ball Daima anime added to the wishlist
Wishlist is full
4=======
Wish List Name: Harry
Anime List:
1. Blue Lock
2. Death Note
3. Dragon Ball Daima
5=======
WishList for Jack created succesfully
6=======
Jujutsu Kaisen anime added to the wishlist
Fullmetal Alchemist anime added to the wishlist
7=======
Wish List Name: Jack
Anime List:
1. Jujutsu Kaisen
2. Fullmetal Alchemist
#
Problem-4
Instance Variable and Method
hard
After your excellent result in quiz-1, amazon got inspired of your coding skills. Now, amazon wanted to hire you to create checkout system for them. There are some constraints that you have to keep in mind while developing the system. Every customer will have a name and budget. They can add items to their cart as long as the total price does not exceed their budget.
public class BillingTester {
public static void main(String[] args) {
Billing cus1 = new Billing();
Billing cus2 = new Billing();
System.out.println("1===========");
cus1.createCustomer("Marry Jane", 400);
cus2.createCustomer("Peter Parker", 200);
System.out.println("2===========");
cus1.cartInfo();
System.out.println("3===========");
cus1.addToCart("Microwave", 150);
cus1.addToCart("PS5", 200);
cus1.addToCart("Controller", 50);
cus1.addToCart("Type-C Charger", 30);
System.out.println("4===========");
cus1.cartInfo();
System.out.println("5===========");
cus1.checkOut();
System.out.println("6===========");
cus2.cartInfo();
System.out.println("7===========");
cus2.addToCart("Chocolates", 20);
cus2.addToCart("Milk", 55);
cus2.addToCart("Bread", 30);
cus2.addToCart("Apple", 15);
cus2.addToCart("Sprite", 15);
System.out.println("8===========");
cus2.cartInfo();
System.out.println("9===========");
cus2.checkOut();
System.out.println("10===========");
cus1.cartInfo();
System.out.println("11===========");
cus2.cartInfo();
}
}
1===========
2===========
Customer Name: Marry Jane
Balance: 400
Cart:
Cart is empty
3===========
Microwave added to cart
PS5 added to cart
Controller added to cart
Insufficient balance
4===========
Customer Name: Marry Jane
Balance: 400
Cart:
1. Microwave - 150
2. PS5 - 200
3. Controller - 50
5===========
Checking out...
Items bought:
1. Microwave - 150
2. PS5 - 200
3. Controller - 50
Total spend: 400
6===========
Customer Name: Peter Parker
Balance: 200
Cart:
Cart is empty
7===========
Chocolates added to cart
Milk added to cart
Bread added to cart
Apple added to cart
Sprite added to cart
8===========
Customer Name: Peter Parker
Balance: 200
Cart:
1. Chocolates - 20
2. Milk - 55
3. Bread - 30
4. Apple - 15
5. Sprite - 15
9===========
Checking out...
Items bought:
1. Chocolates - 20
2. Milk - 55
3. Bread - 30
4. Apple - 15
5. Sprite - 15
Total spend: 135
10===========
Customer Name: Marry Jane
Balance: 0
Cart:
Cart is empty
11===========
Customer Name: Peter Parker
Balance: 65
Cart:
Cart is empty
#
Problem-5
Instance Variable and Method
easy
You are tired of calculating your CGPA, therefore you decided to design a class that will calculated your CGPA for the semester.
public class CgpaTester {
public static void main(String[] args) {
Cgpa sum24 = new Cgpa();
System.out.println("1=========");
sum24.createSemester("Neymar", "Summer-24");
System.out.println("2=========");
sum24.semesterInfo();
System.out.println("3=========");
sum24.addCourse("CSE110", 4.0);
sum24.addCourse("MAT110", 3.0);
sum24.addCourse("ENG101", 3.7);
System.out.println("4=========");
sum24.semesterInfo();
System.out.println("5=========");
System.out.println(sum24.calculateCGPA());
}
}
1=========
2=========
Name: Neymar
Semester: Summer-24
Credit: 0
Courses:
No course added
3=========
CSE110 added to the Summer-24 semester
MAT110 added to the Summer-24 semester
ENG101 added to the Summer-24 semester
4=========
Name: Neymar
Semester: Summer-24
Credit: 9
Courses:
1. CSE110 - 4.00
2. MAT110 - 3.00
3. ENG101 - 3.70
5=========
You have scored 3.566666666666667 in the Summer-24 semester