Lab Assignment 07
Course Code: CSE111
Course Title: Programming Language II
Topic: Array Manipulation and Inheritance Basics
Number of Tasks: 9
[Submit all the Coding Tasks (Task 1 to 8) in the Google Form shared on buX
before the next lab. Submit the Tracing Tasks (Task 9) handwritten to your Lab
Instructors at the beginning of the lab]
[You are not allowed to change the driver/given codes of any of the tasks]
Task 1
Design the BrowserHistory class so that the given output is produced.
Driver Code Output
public class BrowserHistoryTester { 0 web pages visited.
public static void main(String[] args){ 1=========
BrowserHistory chrome = new BrowserHistory(3); No web pages visited yet.
[Link]([Link] + " web pages visited."); 2=========
[Link]("1========="); You have not visited any website yet!
[Link](); 3=========
[Link]("2========="); Visited: [Link]
[Link](); Visited: [Link]
[Link]("3========="); Visited: [Link]
[Link]("[Link]"); History is full. Cannot visit
[Link]("[Link]"); [Link] web page.
[Link]("[Link]"); 4=========
[Link]("[Link]"); 3 web pages visited.
[Link]("4========="); 5=========
[Link]([Link] + " web pages visited."); Browser History:
[Link]("5========="); [Link]
[Link](); [Link]
[Link]("6========="); [Link]
[Link](); 6=========
[Link]("7========="); Current web page: [Link]
[Link](); 7=========
[Link]("8========="); 8=========
[Link]([Link] + " web pages visited."); 2 web pages visited.
[Link]("9========="); 9=========
[Link](); Current web page: [Link]
[Link]("10========="); 10=========
[Link](); 11=========
[Link]("11========="); Current web page: [Link]
[Link](); 12=========
[Link]("12========="); 13=========
[Link](); You have not visited any website yet!
[Link]("13========="); 14=========
[Link](); 0 web pages visited.
[Link]("14=========");
[Link]([Link] + " web pages visited.");
}
}
Task 2
Design the CustomerService class so that the given output is produced.
Driver Code Output
public class CustomerServiceTester { No customers to serve
public static void main(String[] args) { 1=========
CustomerService service = new CustomerService(3); Added: Arthur
[Link](); Added: Bruce
[Link]("1========="); Added: Clark
[Link]("Arthur"); Queue is full. Cannot add Kara
[Link]("Bruce"); 2=========
[Link]("Clark"); Serving Arthur
[Link]("Kara"); Serving Bruce
[Link]("2========="); 3=========
[Link](); Added: Diana
[Link](); Added: Victor
[Link]("3========="); Queue is full. Cannot add Harley
[Link]("Diana"); 4=========
[Link]("Victor"); Serving Clark
[Link]("Harley"); Serving Diana
[Link]("4========="); Serving Victor
[Link](); No customers to serve
[Link]();
[Link]();
[Link]();
}
}
Task 3
Complete the class Circle so that the desired outputs are generated properly.
Given Code Expected Output
public class shapeTester { Name: Mobius Strip
public static void main(String[] args) { Color: Blue
Shape s = new Shape(); =======================
[Link] = "Mobius Strip"; =======================
[Link] = "Blue"; Name: Circle
[Link](); Color: Red
[Link]("======================="); =======================
Circle c = new Circle(); Area of Red Circle: 78.54
[Link]("=======================");
[Link] = "Circle";
[Link] = "Red";
[Link] = 5;
[Link]();
[Link]("=======================");
[Link]();
}
}
public class Shape {
public String name;
public String color;
public void displayInfo() {
[Link]("Name: %s\nColor: %s\n", name, color);
}
}
public class Circle extends Shape {
//Your Code Here
}
Task 4
Complete the class Dog so that the desired outputs are generated properly.
Given Code Expected Output
public class AnimalTester{ 1-------------
public static void main(String args[]){ Legs: 4
Animal a1 = new Animal(); Sound: Not defined
[Link]("1-------------"); 2-------------
[Link](); The dog says hello!
[Link]("2-------------"); 3-------------
Dog d1 = new Dog(); Name: Pammy
[Link] = "Pammy"; Legs: 4
[Link]("3-------------"); Sound: Not defined
[Link]("Name: " + [Link]()); 4-------------
[Link](); 5-------------
[Link]("4-------------"); Legs: 4
[Link]("Bark"); Sound: Bark
[Link]("5-------------");
[Link]();
}
}
public class Animal{
public int legs = 4;
public String sound = "Not defined";
public void details(){
[Link]("Legs: "+legs);
[Link]("Sound: "+sound);
}
}
public class Dog extends Animal{
//Your Code Here
}
Task 5
Given the following classes, write the code for the Cricket_Tournament and the
Tennis_Tournment classes derived from Tournament so that the following output is
generated.
Given Code Expected Output
public class Tester5 {
public static void main(String[] args) { Cricket Tournament Name:
Cricket_Tournament ct1 = new Cricket_Tournament(); Default
[Link]([Link]()); Number of Teams: 0
[Link]("-----------------------"); Type: No type
-----------------------
Cricket_Tournament ct2 = new Cricket_Tournament("IPL", 10, Cricket Tournament Name: IPL
"t20"); Number of Teams: 10
[Link]([Link]()); Type: t20
[Link]("-----------------------"); -----------------------
Tennis Tournament Name: Roland
Tennis_Tournament tt = new Tennis_Tournament("Roland Garros
Garros", 128); Number of Players: 128
[Link]([Link]());
}
}
public class Tournament {
private String name;
public Tournament() {
[Link] = "Default";
}
public Tournament(String name) {
[Link] = name;
}
public void setName(String name){
[Link] = name;
}
public String getName(){
return [Link];
}
}
Task 6
Given the following classes, write the code for the Book and the CD class so that the
following output is printed.
Given Code Expected Output
public class Tester6 { ID: 1 Title: The Alchemist Price: 500
public static void main(String[] args) { ISBN: 97806 Publisher: HarperCollins
Book book = new Book(1, "The Alchemist", 500, "97806", -----------------------
"HarperCollins"); ID: 2 Title: Shotto Price: 300
[Link]([Link]()); Band: Warfaze Duration: 50 minutes
[Link]("-----------------------"); Genre: Hard Rock
CD cd = new CD(2, "Shotto", 300, "Warfaze", 50, "Hard Rock");
[Link]([Link]());
}
}
class Product {
private int id;
private String title;
private int price;
public Product(int id, String title, int price) {
[Link] = id;
[Link] = title;
[Link] = price;
}
public String getIdTitlePrice() {
return "ID: " + id + " Title: " + title + " Price: " + price;
}
}
Task 7
Given the following classes, write the code for the CSEStudent class derived from
Student so that the following output is generated.
Given Code Expected Output
public class StudentTester{ Total CSE Students: 0
public static void main (String args[]){ Available Lab Based Courses:
[Link](); CSE110 CSE111 CSE220 CSE221
[Link]("1--------------"); 1--------------
CSEStudent s1 = new CSEStudent("Bob", 23); Name: Bob
[Link](); ID: 23
[Link]("2--------------"); Courses:
CSEStudent s2 = new CSEStudent("Don", 33); 2--------------
[Link](); Name: Don
[Link]("3--------------"); ID: 33
[Link]("CSE220"); Courses:
[Link]("CSE221"); 3--------------
[Link]("4--------------"); 4--------------
[Link](); Name: Bob
[Link]("5--------------"); ID: 23
[Link](); Courses: CSE220 CSE221
[Link]("6--------------"); 5--------------
[Link]("CSE230"); Total CSE Students: 2
[Link]("7--------------"); Available Lab Based Courses:
[Link](); CSE110 CSE111 CSE220 CSE221
[Link]("8--------------"); 6--------------
[Link]("CSE110"); It is not a lab based course!
[Link](); 7--------------
} Name: Bob
} ID: 23
Courses: CSE220 CSE221
class Student{ 8--------------
public String name; Name: Don
public int id; ID: 33
public String courses = ""; Courses: CSE110
public Student(String n, int i){
name = n;
id = i;
}
public void info(){
[Link]("Name: "+name);
[Link]("ID: "+id);
[Link]("Courses: "+courses);
}
}
Task 8
Given the following classes, write the code for the Player and the Manager classes
derived from SportsPerson class so that the following output is printed. To calculate
the match earnings use the following formula:
1. Player: (total_goal * 1000) + (total_match * 10)
2. Manager: match_win * 1000
Given Code Expected Output
public class PlayerTester { Name: Ronaldo, Team Name: Al-Nassr
public static void main(String[] args) { Team Role: Striker
Player playerOne = new Player("Al-Nassr", "Ronaldo", Total Goal: 25, Total Played: 32
"Striker", 25, 32); Goal Ratio: 0.78
[Link](); Match Earning: 25320K
[Link](); ----------------
[Link]("----------------"); Name: Zidane, Team Name: Real Madrid
Manager managerOne = new Manager("Real Madrid", "Zidane", Team Role: Manager
"Manager", 25); Total Win: 25
[Link](); Match Earning: 25000K
}
}
class SportsPerson {
private String team;
private String name;
public String role;
public double earningPerMatch;
public SportsPerson(String teamName, String name, String role){
[Link] = teamName;
[Link] = name;
[Link] = role;
[Link] = 0;
}
public String getNameTeam() {
return "Name: " + name + ", Team Name: " + team;
}
}
Task 9
1 public class A{
2 public int temp = 4;
3 public int sum = 1;
4 public int y = 2;
5 public void methodA(int m, int n){
6 int x = 0;
7 y = y + m + (temp++);
8 x = x + 2 + n;
9 sum = sum + x + y;
10 [Link](x + " " + y+ " " + sum);
11 }
12 }
13 public class B extends A {
14 public int x = 6;
15 public void methodB(int m, int n){
16 int y =0;
17 y = y + this.y;
18 x = this.y + 2 + temp;
19 methodA(x, y);
20 sum = x + y + [Link];
21 [Link](x + " " + y+ " " + sum);
22 }
23 }
A a1 = new A(); x y sum
[Link](1, 1);
B b1 = new B();
[Link](1, 2);
Ungraded Tasks (Optional)
(You don’t have to submit the ungraded tasks)
Task 1
Complete the class Student so that the desired outputs are generated properly.
Given Code Expected Output
public class Tester5 { Student id: 1
public static void main(String[] args) { Student name: Matthew
Student s1= new Student(1, "Matthew", 1, "Mr. XYZ"); Section id: 1
[Link](); Teacher name: Mr. XYZ
[Link]("======================="); =======================
Student s2= new Student(2, "Wade", 1, "Mr. XYZ"); Student id: 2
[Link](); Student name: Wade
[Link]("======================="); Section id: 1
Student s3= new Student(1, "Logan", 2, "Mr. ABC"); Teacher name: Mr. XYZ
[Link](); =======================
} Student id: 1
} Student name: Logan
Section id: 2
Teacher name: Mr. ABC
public class Section {
public int sec_id;
public String teacher_name;
public int student_id;
public String student_name;
public Section(int id, String name){
this.sec_id = id;
this.teacher_name = name;
}
public void printInfo(){
[Link]("Section id: "+ this.sec_id);
[Link]("Teacher name: "+ this.teacher_name);
}
}
class Student extends Section {
//Your Code Here
}
Task 2
Complete the class Section so that the desired outputs are generated properly.
Given Code Expected Output
public class Tester6 {
public static void main(String[] args) { Number of Sections: 2
Section sec1= new Section("Mr. XYZ"); =======================
Section sec2= new Section("Mr. ABC"); Student id: 1
[Link]("Number of Sections: "+ Section.section_count); Student name: Matthew
[Link]("======================="); Section id: 1
Student s1= new Student(1, "Matthew", sec1.sec_id, Teacher name: Mr. XYZ
sec1.teacher_name); =======================
[Link](); Student id: 2
[Link]("======================="); Student name: Wade
Student s2= new Student(2, "Wade", sec1.sec_id, Section id: 1
sec1.teacher_name); Teacher name: Mr. XYZ
[Link](); =======================
[Link]("======================="); Student id: 1
Student s3= new Student(1, "Logan", sec2.sec_id, Student name: Logan
sec2.teacher_name); Section id: 2
[Link](); Teacher name: Mr. ABC
[Link]("======================="); =======================
[Link]("Number of Students: "+ Student.student_count); Number of Students: 3
}
}
public class Section {
//Your Code Here
}
public class Student extends Section {
public Student (int id, String name, int sec_id, String
teacher_name){
student_count+=1;
this.student_id = id;
this.student_name = name;
this.sec_id= sec_id;
this.teacher_name= teacher_name;
}
public void printStudentInfo(){
[Link]("Student id: "+ this.student_id);
[Link]("Student name: "+ this.student_name);
[Link]("Section id: "+ this.sec_id);
[Link]("Teacher name: "+ this.teacher_name);
}
}