In: Computer Science
THIS IS JAVA PROGRAMMING
1. Create a class named Name that contains the following:
• A private String to represent the first name.
• A private String to represent the last name.
• A public constructor that accepts two values and assigns them to the above properties.
• Public methods named getProperty (e.g. getFirstName) to return the value of the property.
• Public methods named setProperty ( e.g. setFirstName)to assign values to each property by using a single argument passed to each method.
• A public String getFullName() method that will return a properly formatted and spaced String using firstname and lastname. (e.g. “John Doe”)
2. Create a class named Item that contains the following:
• A private integer itemNum to represent the item number.
• A private float cost to represent the cost of the item
• A private String itemDesc to represent the item description
• A public constructor that accepts three values (itemNum, cost, itemDesc,) and assigns them to the properties.
• Public accessor methods getProperty (e.g. getCost) for each property to return the value of the attribute.
• Public mutator methods named setProperty (e.g. setCost) to assign values to the properties by using a single argument passed to each method
3. Allocate an instance of the Item and Name classes in a simple driver application class named Lab6, and verify they are working correctly.
Source code of the program is given below.Detailed comments are written along with the code for better understanding.Screen shot of the code and output are also attached.If find any difficulty, feel free to ask in comment section. Please do upvote the answer.Thank you.
Source code
Name class
public class Name { //declaring instance variable private String fName; private String lName; //Constructor to initialize variables public Name(String fName, String lName) { this.fName = fName; this.lName = lName; } //getter method to get values public String getfName() { return fName; } public String getlName() { return lName; } //setter methods to set values public void setfName(String fName) { this.fName = fName; } public void setlName(String lName) { this.lName = lName; } //getFullName() to get the full name public String getFullName() { return fName+" "+lName; } }
Item class.
public class Item {
//declaring instance variables
private int itemNum;
private float cost;
String itemDesc;
//Constructor to initialize variables
public Item(int itemNum, float cost, String itemDesc) {
this.itemNum = itemNum;
this.cost = cost;
this.itemDesc = itemDesc;
}
//getter method to get values
public int getItemNum() {
return itemNum;
}
public float getCost() {
return cost;
}
public String getItemDesc() {
return itemDesc;
}
//setter method to set values
public void setItemNum(int itemNum) {
this.itemNum = itemNum;
}
public void setCost(float cost) {
this.cost = cost;
}
public void setItemDesc(String itemDesc) {
this.itemDesc = itemDesc;
}
}
Driver class Lab6
public class Lab6 { public static void main(String[] args) { //creating an Item instance Item i=new Item(100,10,"Red ink pen"); //printing item details System.out.println("Item details: "); System.out.println("Item Number: "+i.getItemNum()); System.out.println("Item Cost: "+i.getCost()); System.out.println("Item Description: "+i.getItemDesc()); //creating an Name instance Name n=new Name("Albert","Einstein"); //printing full name System.out.println("Full name is :"+n.getFullName()); } }
Screen shot of the code
Screen shot of the output.