Question

In: Computer Science

Create a Class with Data Fields and Methods in Java. Provide a Java coding solution for...

Create a Class with Data Fields and Methods in Java. Provide a Java coding solution for the following:

1. Name the class SalonServices

2. Add private data fields: a. salonServiceDescription – This field is a String type b. price - This field is a double type

3. Create two methods that will set the field values.

a. The first method setSalonServiceDescription() accepts a String parameter defined as service and assigns it to the salonServiceDescription. The method is not static

b. The second method setPrice() accepts a double parameter defined as salonPrice and assigns it to the price field. The method is not static

4. Create two methods that retrieve or get the field value.

a. The first method is getSalonServiceDescription()

b. The other method is getPrice()

Create an application to run objects of the class SalonService

This will be an interactive program. Hint: java.util import

1. Name the class CreateSalonServices

2. The main() method will contain two variables from the user input

a. String service

b. double price

3. Create an object for SalonServices and name it service1

4. Create an object for SalonServices and name it service2

5. Create an object that uses the built-in Java Scanner class

6. Prompt the user to enter a service. You will store the response in the variable service

a. User will enter “massage” as the service during the execution of the program

7. Prompt the user to enter a price. You will store the response in the variable price

a. User will enter 65.00 as the price during the execution of the program.

8. Write the statement that will send the user input for service to the setSalonServiceDescription() method for service1 object.

9. Write the statement that will send the user input for price to the setPrice() method for service1 object.

10. Type ______.nextLine() The ____ will be replaced with the name of your scanner object sc , input, etc.

11. User prompted for second service information. Prompt the user to enter a service. You will store the response in the variable service

a. User will enter “facial” as the service during the execution of the program

12. Prompt the user to enter a price. You will store the response in the variable price.

a. User will enter 45.00 as the price during the execution of the program.

13. Write the statement that will send the user input for service to the setSalonServiceDescription() method for service2 object.

14. Write the statement that will send the user input for price to the setPrice() method for service2 object

15. Display the details for service1 object on two lines:

a. Line 1 will contain the string literal: “Details for service one: “

b. Line 2 will contain the description and price information

16. Display the details for service2 object on two lines

a. Line 1 will contain the string literal: “Details for service two: “

b. Line 2 will contain the description and price information

Compile and Execute the program.

Solutions

Expert Solution

Code for your program is provided below. Code is explained thoroughly in code comments. You can also refer to screenshot of properly indented code in IDE, which i have attached in the last. Output screenshot is also provided.

If you need any further clarification please feel free to ask in comments. I would really appreciate if you would let me know if the explanation provided is upto your satisfaction.

########################################################################

CODE

import java.util.Scanner;   //importing to use scanner for input

//class SalonServices
class SalonServices
{
        private String salonServiceDescription;   //to store description
        private double price;   //to store price
        //function to get the desription of service
        public String getSalonServiceDescription() {
                return salonServiceDescription;
        }
        //function to set description of service
        public void setSalonServiceDescription(String salonServiceDescription) {
                this.salonServiceDescription = salonServiceDescription;   //setting service description with given parameter
        }
        //method to return  the price 
        public double getPrice() {
                return price;
        }
        //method to set the price
        public void setPrice(double price) {
                this.price = price;
        }
}

//class CreateSalonServices
public class CreateSalonServices {
        //main
        public static void main(String[] args) 
        {
                Scanner scan=new Scanner(System.in);    //to take input
                String service="";    //to store service
                double price=0; //to store price
                
                //creating two objects of SalonServices
                SalonServices service1=new SalonServices();
                SalonServices service2=new SalonServices();
                
                System.out.print("Enter the service: ");
                service=scan.nextLine();   //read service
                System.out.print("Enter the price: ");
                price=scan.nextDouble();        //read price
                
                service1.setSalonServiceDescription(service);   //set service description
                service1.setPrice(price);       //set price
                
                scan.nextLine();   //flush the buffer of scanner to take input again
                
                System.out.print("Enter the service: ");
                service=scan.nextLine();        //read service
                System.out.print("Enter the price: ");
                price=scan.nextDouble();        //read price
                
                service2.setSalonServiceDescription(service);   //set service desription
                service2.setPrice(price);       //set price
                
                System.out.println("\nDetails for service one: ");
                //display details of service1 using getter methods
                System.out.println("Service: "+service1.getSalonServiceDescription()+"     Price: "+service1.getPrice());
                
                System.out.println("\nDetails for service two: ");
                //display details of service2 using getter methods
                System.out.println("Service: "+service2.getSalonServiceDescription()+"     Price: "+service2.getPrice());
                
                scan.close();  //closing the resource scanner
        }
}

########################################################################

OUTPUT

###########################################################################

SCREENSHOT OF CODE IN IDE


Related Solutions

java Objective: Create a class. Create objects. Use methods of a class. Create a class BankAccount...
java Objective: Create a class. Create objects. Use methods of a class. Create a class BankAccount to represent a bank account according to the following requirements: A bank account has three attributes: accountnumber, balance and customer name. Add a constructor without parameters. In the initialization of the attributes, set the number and the balance to zero and the customer name to an empty string. Add a constructor with three parameters to initialize all the attributes by specific values. Add a...
Java Coding Background You will create a Java class that simulates a water holding tank. The...
Java Coding Background You will create a Java class that simulates a water holding tank. The holding tank can hold volumes of water (measured in gallons) that range from 0 (empty) up to a maximum. If more than the maximum capacity is added to the holding tank, an overflow valve causes the excess to be dumped into the sewer system. Assignment The class will be named HoldingTank. The class attributes will consist of two int fields – current and maxCapacity....
JAVA CODING PLEASE Create a class SportsCar that inherits from the Car class (CAR CLASS LISTED...
JAVA CODING PLEASE Create a class SportsCar that inherits from the Car class (CAR CLASS LISTED BELOW THIS QUESTION). Include the following: A variable Roof that holds the type of roof (Ex: Convertible, Hard-Top, Soft-Top) A variable Doors that holds the car’s number of doors (Ex: 2, 4) Implement the ChangeSpeed method to add 20 to the speed each time it is called. Add exception handling to the ChangeSpeed method to keep the speed under 65. Implement the sound method...
write program in java Create a class named PersonalDetails with the fields name and address. The...
write program in java Create a class named PersonalDetails with the fields name and address. The class should have a parameterized constructor and get method for each field.  Create a class named Student with the fields ID, PersonalDetails object, major and GPA. The class should have a parameterized constructor and get method for each field. Create an application/class named StudentApp that declare Student object. Prompts (GUI input) the user for student details including ID, name, address, major and GPA....
Code in Java 1. Create a class Flower with data: Name, Price, Color and properly methods....
Code in Java 1. Create a class Flower with data: Name, Price, Color and properly methods. 2. Create another class named ListFlower. This class manages a collection of Flower (may be LinkedList) named a. Implementing some methods for ListFlower: Add: add new item of Flower to a Display: display all items of a sort(): sort as descending by Price and display all items of a search(Flower f): check and return whether f is exists in a or not. delete(int pos):...
Coding Java Assignment Write the following static methods. Assume they are all in the same class....
Coding Java Assignment Write the following static methods. Assume they are all in the same class. Assume the reference variable input for the Scanner class and any class-level variables mentioned are already declared. All other variables will have to be declared as local unless they are parameter variables. Use printf. A method that prompts for the customer’s name and returns it from the keyboard. A method called shippingInvoice() that prompts for an invoice number and stores it in a class...
Create java Class with name Conversion. Instructions for Conversion class: The Conversion class will contain methods...
Create java Class with name Conversion. Instructions for Conversion class: The Conversion class will contain methods designed to perform simple conversions. Specifically, you will be writing methods to convert temperature between Fahrenheit and Celsius and length between meters and inches and practicing overloading methods. See the API document for the Conversion class for a list of the methods you will write. Also, because all of the methods of the Conversion class will be static, you should ensure that it is...
This is 1 java question with its parts. Thanks! Create a class named Lease with fields...
This is 1 java question with its parts. Thanks! Create a class named Lease with fields that hold an apartment tenant’s name, apartment number, monthly rent amount, and term of the lease in months. Include a constructor that initializes the name to “XXX”, the apartment number to 0, the rent to 1000, and the term to 12. Also include methods to get and set each of the fields. Include a nonstatic method named addPetFee() that adds $10 to the monthly...
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...
INVENTORY CLASS You need to create an Inventory class containing the private data fields, as well...
INVENTORY CLASS You need to create an Inventory class containing the private data fields, as well as the methods for the Inventory class (object). Be sure your Inventory class defines the private data fields, at least one constructor, accessor and mutator methods, method overloading (to handle the data coming into the Inventory class as either a String and/or int/float), as well as all of the methods (methods to calculate) to manipulate the Inventory class (object). The data fields in the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT