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 Q1: Create a class named Triangle, the class must contain: Private data fields base and...
Java Q1: Create a class named Triangle, the class must contain: Private data fields base and height with setter and getter methods. A constructor that sets the values of base and height. A method named toString() that prints the values of base and height. A method named area() that calculates and prints the area of a triangle. Draw the UML diagram for the class. Implement the class. Q2: Write a Java program that creates a two-dimensional array of type integer...
In Java...create a class Realestate.java with the following fields: location, price, and description. Create a class...
In Java...create a class Realestate.java with the following fields: location, price, and description. Create a class RealestateFinder.java that reads in real estate data from a CSV file (RealestateList.csv). Then provide the user with the following options: Sort per Price, Sort per Location, and Exit. Use ArrayList and Arrays.sort to perform the sorts and display the data to the user.
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 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....
Array of Hope! Write code for a Java class ArrayPair with the following fields and methods:...
Array of Hope! Write code for a Java class ArrayPair with the following fields and methods: ArrayPair - arr1 [] : int - arr2 [] : int Fields - length1 : int - length2 : int + ArrayPair (int arraySize) Methods + add (int arrNumber, int value) : void + remove (int arrNumber) : void + equal () : boolean + greater () : boolean + print () : void Thus, there are two arrays of integers in the class...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT