Question

In: Computer Science

THIS IS JAVA PROGRAMMING 1. Create a class named Name that contains the following: • A...

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.

Solutions

Expert Solution

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.


Related Solutions

PUT IN JAVA PROGRAMMING The StockB class: Design a class named StockB that contains the following...
PUT IN JAVA PROGRAMMING The StockB class: Design a class named StockB that contains the following properties: • Name of company • Number of shares owned • Value of each share • Total value of all shares and the following operations: • Acquire stock in a company • Buy more shares of the same stock • Sell stock • Update the per-share value of a stock • Display information about the holdings • The StockB class should have the proper...
JAVA PROGRAMMING. In this assignment, you are to create a class named Payroll. In the class,...
JAVA PROGRAMMING. In this assignment, you are to create a class named Payroll. In the class, you are to have the following data members: name: String (5 pts) id: String   (5 pts) hours: int   (5 pts) rate: double (5 pts) private members (5 pts) You are to create no-arg and parameterized constructors and the appropriate setters(accessors) and getters (mutators). (20 pts) The class definition should also handle the following exceptions: An employee name should not be empty, otherwise an exception...
Put In Java Programming The TicketMachine class: Design a class named TicketMachine that contains: • A...
Put In Java Programming The TicketMachine class: Design a class named TicketMachine that contains: • A double data field named price (for the price of a ticket from this machine). • A double data field named balance (for the amount of money entered by a customer). • A double data field named total (for total amount of money collected by the machine). • A constructor that creates a TicketMachine with all the three fields initialized to some values. • A...
PUT IN JAVA PROGRAMMING The Stock class: Design a class named Stock that contains: • A...
PUT IN JAVA PROGRAMMING The Stock class: Design a class named Stock that contains: • A string data field named symbol1 for the stock’s symbol. • A string data field named name for the stock’s name. • A double data field named previousClosingPrice that stores the stock price for the previous day. • A double data field named currentPrice that stores the stock price for the current time. • A constructor that creates a stock with the specified symbol and...
THIS IS JAVA PROGRAMMING Design a class named Account (that contains 1. A private String data...
THIS IS JAVA PROGRAMMING Design a class named Account (that contains 1. A private String data field named id for the account (default 0). 2. A private double data field named balance for the account (default 0). 3. A private double data field named annualInterestRate that stores the current interest rate (default 0). 4. A private Date data field named dateCreated that stores the date when the account was created. 5. A no-arg constructor that creates a default account. 6....
Java Programming Create a class named Problem1, and create a main method, the program does the...
Java Programming Create a class named Problem1, and create a main method, the program does the following: - Prompt the user to enter a String named str. - Prompt the user to enter a character named ch. - The program finds the index of the first occurrence of the character ch in str and print it in the format shown below. - If the character ch is found in more than one index in the String str, the program prints...
Create a class named Horse that contains the following data fields: name - of type String...
Create a class named Horse that contains the following data fields: name - of type String color - of type String birthYear - of type int Include get and set methods for these fields. Next, create a subclass named RaceHorse, which contains an additional field, races (of type int), that holds the number of races in which the horse has competed and additional methods to get and set the new field. ------------------------------------ DemoHorses.java public class DemoHorses {     public static void...
In java: -Create a class named Animal
In java: -Create a class named Animal
Create a Java class named Package that contains the following: Package should have three private instance...
Create a Java class named Package that contains the following: Package should have three private instance variables of type double named length, width, and height. Package should have one private instance variable of the type Scanner named input, initialized to System.in. No-args (explicit default) public constructor, which initializes all three double instance variables to 1.0.   Initial (parameterized) public constructor, which defines three parameters of type double, named length, width, and height, which are used to initialize the instance variables of...
In java, create a class named Contacts that has fields for a person’s name, phone number...
In java, create a class named Contacts that has fields for a person’s name, phone number and email address. The class should have a no-arg constructor and a constructor that takes in all fields, appropriate setter and getter methods. Then write a program that creates at least five Contact objects and stores them in an ArrayList. In the program create a method, that will display each object in the ArrayList. Call the method to demonstrate that it works. Include javadoc...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT