CIS144 - Lab Assignment #5 OOP Encapsulation: Creating Your Own Class with Java
ID: 3741711 • Letter: C
Question
CIS144 - Lab Assignment #5
OOP Encapsulation: Creating Your Own Class with Java
Date Assigned: Week 5 Date Due: Week 7 Points: 50
Write a Java application, and an additional class to represent some real-world entity such as a technology item, an animal, a person, a vehicle, etc. Keep in mind that a class is a model in code of something real or imagined, which has attributes (member variables) and behaviors (member methods).
Note: I will go through and post examples of a SmartPhone class and a FootballPlayer class in class, so choose something other than these.
The class will:
a. Create a total of 5 member variables for the class, selecting the appropriate data types for each field. For example, a class to represent a lamp might include color, price, height, numBulbs, batteryOperated. Each of these 5 variables need a data type.
b. Include at least three different constructor methods, in addition to the default constructor (0 argument constructor). The constructor is a function (method) which allocates memory and initialized the member variables specified in (a.).
c. Include getters/setters for to serve as as mutators and accessors for each variable. Name these appropriately such as setColor & getColor, setPrice & getPrice, setHeight & getHeight, setNumBulbs and getNumBulbs, and setBatteryOperated & getBatteryOperated.
d. Create a member function showValues() to display the values of an object in a formatted manner.
e. Create at least 2 other member functions (methods) for the class that will perform some operation on the data (i.e. calculations or additional report/display of output). For example, turnLampOn, changeBulb, etc.
The Java application class (with a main method) will:
a. Instantiate at least three objects (using each of the constructors at least once) with your program.
Note: Set the remaining data with the mutator methods.
b. Store the data from the individual objects into an ArrayList (or some other dynamic data structure of objects.
c. Utilize the showValues() method and each of the two methods on each of your objects to demonstrate their use.
Required Output: Generate output samples demonstrating all of your member functions, adequately testing them and showing their functionality (i.e. inputting values, outputting values (displaying them), performing calculations, etc.).
Explanation / Answer
ScreenShot
--------------------------------------------------------------------------------------
Program
/** Packages**/
import java.util.*;
import java.lang.*;
import java.io.*;
/** class lamp to understand class concept**/
class Lamp{
/**Instance variables**/
String color;
double price;
double height;
int numBulbs;
String batteryOperated;
/**Default constructor**/
public Lamp(){
color="";
price=0.00;
height=0.00;
numBulbs=0;
batteryOperated="Yes";
}
/**Parameterized constructor**/
public Lamp(String c,double p,double h,int nb,String bo){
color=c;
price=p;
height=h;
numBulbs=nb;
batteryOperated=bo;
}
/**Copy constructor**/
public Lamp(Lamp l){
color=l.color;
price=l.price;
height=l.height;
numBulbs=l.numBulbs;
batteryOperated=l.batteryOperated;
}
/**Setters or mutators**/
public void setColor(String c){
color=c;
}
public void setPrice(double p){
price=p;
}
public void setNumBulbs(int nb){
numBulbs=nb;
}
public void setBatteryOperated(String bo){
batteryOperated=bo;
}
public void setHeight(double h){
height=h;
}
/**Getters or Accessors**/
public String getColor(){
return color;
}
public double getPrice(){
return price;
}
public int getNumBulbs(){
return numBulbs;
}
public String getBatteryOperated(){
return batteryOperated;
}
public double getHeight(){
return height;
}
/**Display method**/
public void showValues(){
System.out.println("The color of the Lamp is "+this.color);
System.out.println("The Price of the Lamp is $"+this.price);
System.out.println("The Height of the Lamp is "+this.height);
System.out.println("The Number of the bulbs inthe lamp is "+this.numBulbs);
System.out.println("Battery Operated or not = "+this.batteryOperated);
}
/**change the number of bulbs count to 10 **/
public int changeBulbCount(){
numBulbs=10;
return numBulbs;
}
/**change the number of lamp color to yellow **/
public String changeLampColor(){
color="Yellow";
return color;
}
}
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
/** object creation1 **/
Lamp lamp1=new Lamp();
System.out.println("First object return values :");
System.out.println("-----------------------------");
lamp1.showValues();
/** object creation2 **/
Lamp lamp2=new Lamp("Green",15.00,10.50,50,"No");
System.out.println(" Second object return values :");
System.out.println("-----------------------------");
lamp2.showValues();
/** object creation3 **/
Lamp lamp3=new Lamp(lamp2);
lamp3.changeBulbCount();
lamp3.changeLampColor();
System.out.println(" Third object return values :");
System.out.println("-----------------------------");
lamp3.showValues();
/**ArrayList creation*/
Lamp lamp4=new Lamp();
lamp4.setColor("Violet");
lamp4.setPrice(25.00);
lamp4.setHeight(9.00);
lamp4.setNumBulbs(40);
lamp4.setBatteryOperated("yes");
List<Lamp> LampList= new ArrayList<Lamp>();
LampList.add(lamp4);
}
}
--------------------------------------------------------------------------------------------------
Output