In: Computer Science
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.
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