You are on page 1of 5

LAB 7: INHERITANCE

Mã Môn học/Course No.: CMU-CS 303


Tên Môn học/Course Name: Fundamentals Of Computing 1
Năm học/Academic Year: 2019-2020
Học kỳ/Semester: II
Khoa hay Viện/Faculty International School
Name:

Project naming convention


 Create a project named: Lab07_Inheritance_ID
 With ID stands for student’s identify
 Ex: If your student’s ID is 9600278, so your project name will be:
Lab07_Inheritance _9600278
 Pay attention to “_” (underscore)
Package naming convention: Each project has one or many packages.
 Each package has a specific name
 Each Lab has one package name. Remember to name the packages as follow:
labNo_ID
 With No is a number of each lab, ID stands for student’s identify
 Ex: lab01_9600278, lab02_9600278
 Remember to use lowercase l
Class naming convention (Files): Each package has one or many classes
 Each class has a specific name
 Remember to name the classes (classname) as follow:
LabNo_ID and TestLabNo_ID
 With No is a number of each lab, ID stands for student’s identify
 Ex: Lab01_9600278, TestLab01_9600278, Lab02_9600278,
TestLab02_9600278
 Remember to capitalize each word L, T and L
1. Design a class named Person and its two subclasses named Student and Employee.
Make Faculty and Staff subclasses of Employee. A person has a name, address, phone
number, and email address. A student has a class status (freshman, sophomore, junior,
or senior). Define the status as a constant. An employee has an office, salary, and
date-hired. A faculty member has office hours and a rank. A staff member has a title.
Override the out method in Student, Employee, Faculty, and Staff classes to display
the class name and the person's name.

FOR EXERCISES 2 TO 4, CONSIDER THE FOLLOWING CLASS:


public class Game{
pr ivat e Str ing descr ipt ion;
Game( St ring newDescr ipt ion )
{
set Descr ipt ion( newDescr ipt ion ) ;
}
public St r ing get Descr ipt ior i( )
{
ret urn descr ipt ion;
}
public vo id set Descr i pt i on( St r ing newDescr ipt io n )
{ descr ipt io n = newDescr ipt ion;
}
public St r ing t oSt ring( )
{
ret urn ( "descr ipt ion: ” + descr ipt io n );
}
}
2. Write a class encapsulating a PC-based game, which inherits from Game. A PC-based
game has the following additional attributes: the minimum megabytes of RAM
needed to play the game, the number of megabytes needed on the hard drive to
install the game, and the minimum GHz performance of the CPU. Code the
constructor and the toString method of the new class. You also need to include a
client class to test your code.
3. Write a class encapsulating a trivia game, which inherits from Game. A trivia game has
the following additional attributes: the ultimate money prize, and the number of
questions that must be answered to win the ultimate money. Code the constructor
and the toString method of the new class. You also need to include a client class to
test your code.
4. Write a class encapsulating a board game, which inherits from Game. A board game
has the following additional attributes: the minimum number of players, the
maximum number of players, and whether there is a time limit to finish the game.
Code the constructor and the toString method of the new class. You also need to
include a client class to test your code.
For Exercises 5 to 6, consider the following class:
public class Store {
public final double SALESJAXJATE = 0.06;
private String name;
public Store( String newName )
{
setName( newName );
}
public String getName( )
{
return name;
}
}

5. Write a class encapsulating a web store, which inherits from Store. A web
store has the following additional attributes: an Internet address and the
programming language in which the website was written. Code the
constructor and the toString method of the new class. You also need to
include a client class to test your code.
6. Write a class encapsulating a restaurant, which inherits from Store. A restaurant
has the following additional attributes: how many people are served every year
and the average price per person. Code the constructor and the toString method of
the new class; also code a method returning the average taxes per year. You also
need to include a client class to test your code.
ABSTRACT CLASS
7. Write an abstract superclass encapsulating a shape: A shape has two abstract
methods: one returning the perimeter of the shape, another returning the area of
the shape. It also has a constant field named PI. This class has two non-abstract
subclasses: one encapsulating a circle, and the other encapsulating a rectangle. A
circle has one additional attribute, its radius. A rectangle has two additional
attributes, its width and height. You also need to include a client class to test
these two classes.
8. Write an abstract superclass encapsulating a number; this class has 4 abstract void
methods: Add, subtract, multifly, divide. This class has two non-abstract sub-
classes: one encapsulating a fraction number, and the other encapsulating a
complex number.
9. Write an abstract superclass encapsulating some food; it has two attributes: its
description and the number of calories per serving. It also has an abstract method
taking a number of servings as a parameter and returning the number of calories.
This class has two non- abstract subclasses: one encapsulating a liquid food (such
as a drink, for instance), and the other encapsulating a fruit. A liquid food has an
additional attribute: its viscosity. A fruit has an additional attribute: its season.
You also need to include a client class to test these two classes.

You might also like