Starting from:

$25

2110215PROG-Lab 2 Solved

Lab 2: Java Inheritance 


1. Problem Statement                                                                      

Space is one of the places that we want to go to in our childhood. However, this kind of dream costs us lots of money. Being just a computer geek, we are going to create our own virtual solar system. 


Figure 1: Solar System 

Our Solar System consists of many planets orbiting around the Sun. For simplicity, we will choose only two planets: Earth and Saturn. In addition, the orbit will be simple too because the planet can only orbit around these 4 positions (shown in Figure 2) that far away from the Sun defined by orbit radius (each planet may have different orbit radius). 

Figure 2: Orbit System 

However, the Saturn class is still incomplete. So it’s your task to help fulfill our childhood dream.

For better understanding of this, there is a SolarSystem class in package solarsystem which will provide the real usage of the planets. The document about how to use it is available as SolarSystem.pdf.

2. Implementation Detail 

          The class-package is summarized below 

Figure 4 Class diagram 

You must write java class Saturn from scratch using UML diagram specified above. 

* In the following class description, only details of IMPORTANT fields and methods are given. * 

4.1 Package solar 

Class Coordinate /*Fill Code*/ 
Field 

▪ - int x - The position of axis X.  

▪ - int y - The position of axis Y. 


Constructor 

▪ + Coordinate() - Set related fields with default values (All field will be set to 0). 

▪ + Coordinate(int x, int y) - Set related fields with the given parameters.
 

Method 

▪ + int getX() - Return the position of axis X. 

▪ + int getY() - Return the position of axis Y.

▪ + void setX(int x) - Set the position of axis X equals to x.

▪ + void setY(int y) - Set the position of axis Y equals to y.

 

Class Planet /*Fill Code*/ 
Field 

▪ # Coordinate coordinate - The current coordinate of this planet.  

▪ # Coordinate orbitCenterCoordinate - The orbit center coordinate used for orbiting around. 

▪ # int orbitRadius - The orbit radius of this planet (the distance from the planet’s coordinate to the orbit center coordinate).  
 

Constructor 

▪ + Planet() - Set related fields with default values (coordinate will be set to (1,0) which will correspond to orbitRadius with value 1). 

▪ + Planet(int orbitRadius) - Set related fields with the given parameters; note that the orbitRadius must be at least 1.

Method 

▪ + Coordinate getCoordinate() - Return current coordinate. 

▪ + Coordinate getOrbitCenterCoordinate() - Return orbit center coordinate. 

▪ + int getOrbitRadius() - Return the orbit radius.

▪ + boolean orbit() - Return true if the orbit can occur. The planet will orbit only to 4 positions (far from center with orbit radius) clockwisely around the center. Otherwise, return false.

  
Class Earth /*Fill Code*/ 
Field 

▪ - int wasteLevel - The Earth’s waste level. 


Constructor 

▪ + Earth() - Set related fields with default values. The waste level will be set to zero. 

▪ + Earth(int orbitRadius, int wasteLevel) - Set related fields with the given parameters; note that wasteLevel can’t be below 0.

 
Method 

▪ + int getWasteLevel() - Return Earth’s waste level. 

▪ + boolean orbit() - Return true if the orbit can occur. For Earth, the orbit can occur only when the Earth’s waste level is less than or equal 5. Otherwise, return false. 


Class Saturn /*Fill Code*/ 
Field 

▪ - int speed - Number of times that the Saturn will orbit when the orbit command is executed. 


Constructor 

▪ + Saturn() - Set related fields with default values. The speed will be set to zero. 

▪ + Saturn(int orbitRadius, int speed) - Set related fields with the given parameters; note that speed can’t be below zero.

Method 

▪ + int getSpeed() - Return Saturn’s speed. 

▪ + boolean orbit() - Return true if the orbit can occur. For Saturn, the orbit can occur only when the speed is more than zero (the number of orbit times will equal to the value of speed eg. speed is 2 means orbits 2 times). Otherwise, return false. 

Class Application /*Fill Code*/  
Field 

▪ - ArrayList<Planet> planets - An ArrayList of Planet. 


Constructor 

▪ + String printPlanet(Planet planet) - Return planet’s specific attribute in the format specified below. If the planet isn’t Earth or Saturn return empty String. 

o {PLANET_NAME}’s {SPECIFIC_ATTRIBUTE} is {SPECIFIC_ATTRIBUTE_VALUE}

▪ eg. “Earth’s Waste Level is 4 ” 

▪ eg2. “Saturn’s Speed is 2 ” 

 

Method 

▪ + void main() - Create Earth with orbit radius equals 1 and waste level equals 4 and Saturn with orbit radius equals 2 and speed equals 2. Then, add both planets to the ArrayList of Planet. Finally, use printPlanet() to show the specific attribute of each planet (Note that: toString() method for each planet is forbidden). 

 

4.2 Package SolarSystem 

             

             Class solarsystem 

 

Field 

●      /*Fill Code*/ + ArrayList<Planet> planets - An ArrayList of planet 

●      + int X_RANGE - range x-axis in solar system. 

●      + int Y_RANGE - range y-axis in solar system. 

 

                Method 

●      + void main() - show command menu 

●      + void addPlanet(Planet planet) - /*Fill Code*/  Add planet if the planet does not existed and use printMap() to show solar system. 

●      + boolean doesPlanetExist(Planet planet) - /*Fill Code*/  If planet does existed 

“print(planet.getClass().toString().split(" ")[1] + " exists !!!")”  and return true else return false. 

●      + void printMap() - Show solar system. 

                                 

Score Criteria (13) (will be rounded to 5) 

ApplicationTest 

 testPrintEarth 

 testPrintOtherPlanet 

 testPrintSaturn 

EarthTest 

 testConstructor1 

 testConstructor2 

 testOrbit 

 testgetWasteLevel 

SaturnTest 

 testConstructor1 

 testConstructor2 

 testGetSpeed 

 testOrbit 

 

More products