Question
Creating a Class that Contains Instance Fields and Methods You make a class to store information about event services offered at Paradise Day Spa. 1.
Creating a Class that Contains Instance Fields and Methods
You make a class to store information about event services offered at Paradise Day Spa.
1. In Java Editor, type the following class header and the curly braces to surround the class body:
public class SpaService { }
2. Between the curly braces for the class, insert two private data fields that will hold data about a spa service: private String servicedescription; private double price;
3. Within the class's curly braces and after the field declarations, enter the following two methods that set the field values. The setServiceDescription() method accepts a String parameter and assigns it to the serviceDescription field for each object that eventually will be instantiated. Similarly, the setPrice() method accepts a double parameter and assigns it to the price field. Note that neither of these methods is static.
public void setServiceDescription(String service) { serviceDescription = service; } public void setPrice(double pr) { price = pr; }
4. Next, add two methods that retrieve the field values as follows:
public String getServiceDescription() { return serviceDescription; } public double getPrice() { return price; }
Creating a Class that Contains Instance Fields and Methods
You create a class to store information about event services offered at Paradise Day Spa.
1. In Java Editor, type the following class header and the curly braces to surround the class body:
public class SpaService { }
2. Between the curly braces for the class, insert two private data fields that will hold data about a spa service: private String servicedescription; private double price;
3. Within the class's curly braces and after the field declarations, enter the following two methods that set the field values. The setServiceDescription() method accepts a String parameter and assigns it to the serviceDescription field for each object that eventually will be instantiated. Similarly, the setPrice() method accepts a double parameter and assigns it to the price field. Note that neither of these methods is static.
public void setServiceDescription(String service) { serviceDescription = service; } public void setPrice(double pr) { price = pr; }
4. Next, add two methods that retrieve the field values as follows:
public String getServiceDescription() { return serviceDescription; } public double getPrice() { return price; }
Declaring and Using Objects
In the above activity, you created a class named SpaService. Now you create an application that instantiates and uses SpaService objects. 1. Open a new file in your java editor, and type the import statement needed for an interactive program that accepts user keyboard input:
import java.util.Scanner;
2. Make the shell for a class named CreateSpaServices:
public class CreateSpaServices { } 3. Between the curly braces of the CreateSpaServices class, create the shell for a main() method for the application:
public static void main(String[] args) { }
4. Within the main() method, declare variables to hold a service description and price that a user can enter from the keyboard:
String service; double price;
5. Next, declare three objects. Two are SpaService objects that use the class you created in the last set of actionable steps. The third object uses the built-in Java Scanner class. Both classes use the new operator to allocate memory for their objects, and both call a constructor that has the same name as the class. The difference is that the Scanner constructor requires an argument (System.in), but the SpaService class does not.
SpaService firstService = new SpaService(); SpaService secondService = new SpaService(); Scanner keyboard = new Scanner(System.in);
6. In the next statements, you prompt the user for a service, accept it from the keyboard, prompt the user for a price, and accept it from the keyboard.
System.out.print("Enter service >> "); service = keyboard.nextLine(); System.out.print("Enter price >> ");
price = keyboard.nextDouble();
7. Recall that the setServiceDescription() method in the SpaService class is nonstatic, meaning it is used with an object, and that it requires a String argument. Write the statement that sends the service the user entered to the setServiceDescription() method for the firstService object:
firstService.setServiceDescription(service);
8. Similarly, send the price the user entered to the setPrice() method for the firstService object. Recall that this method is nonstatic and requires a double argument.
firstService.setPrice(price);
9. Make a call to the nextLine() method to remove the Enter key that remains in the input buffer after the last numeric entry. Then repeat the prompts, and accept data for the second SpaService object.
keyboard.nextLine(); System.out.print("Enter service >> "); service = keyboard.nextLine(); System.out.print("Enter price >> "); price = keyboard.nextDouble(); secondService.setServiceDescription(service); secondService.setPrice(price);
10. Display the details for the firstService object.
System.out.println("First service details:"); System.out.println(firstService.getServiceDescription() + " $" + firstService.getPrice());
11. Display the details for the secondService object.
System.out.println("Second service details:"); System.out.println(secondService.getServiceDescription() + " $" + secondService.getPrice());
Adding a Constructor to a Class
1. Open the SpaService.java file that you created in the previous section earlier in this activity. 2. After the field declarations, and before the method declarations, insert an explicit default constructor that sets serviceDescription to "XXX" and price to 0. Because numeric fields in objects are set to 0 by default, the last assignment is not really necessary. However, programmers sometimes code a statement like the one that sets the price to 0 so that their intentions are clear to people reading their programs.
public SpaService() { serviceDescription = "XXX"; price = 0; }
3. Save the class and compile it. 4. Open the CreateSpaServices.java file. Comment out the seven statements that prompt for, receive, and set the values for the secondService object by placing double slashes at the start of their lines, as shown below. By commenting out these lines, you change the program so that the user does not enter values for the secondService object. Instead, the values assigned by the constructor are the final values for the object. // keyboard.nextLine(); // System.out.print("Enter service >> "); // service = keyboard.nextLine(); // System.out.print("Enter price >> "); // price = keyboard.nextDouble(); // secondService.setServiceDescription(service); // secondService.setPrice(price);
Understanding that Classes Are Data Types
In this activity, you modify the CreateSpaServices class to include a method for data entry. This change makes the main() method shorter, gives you the ability to reuse code and shows that an object of the SpaService class data type can be returned from a method as easily as a primitive data type. 1. Open the CreateSpaServices.java file if it is not still open in your text editor. 2. Delete the declarations for service, price, and keyboard. Declarations for these variables will now be part of the data entry method that you will create. 3. Delete the six statements that prompt the user and get values for the firstService objects. Also delete the seven statements that prompt the user and retrieve data for the secondService object. You commented out these statements in the previous activity section. 4. In place of the statements you just deleted, insert two new statements. The first sends a copy of the firstService object to a method named getData(). The method returns a SpaService object that will be filled with appropriate data, and this object is assigned to firstService. The second statement does the same thing for secondService.
firstService = getData(firstService);
secondService = getData(secondService);
5. After the closing curly brace for the main() method, but before the closing curly brace for the class, start the following public static getData() method. The header indicates that the method both accepts and returns a SpaService object. Include the opening curly brace for the method, and make declarations for service, price, and keyboard. public static SpaService getData(SpaService s) { String service; double price; Scanner keyboard = new Scanner(System.in);
6. Continue the method by prompting the user for and accepting service and its price. Include a final call to nextLine() so that the input buffer is cleared after the last numeric entry.
System.out.print("Enter service >> "); service = keyboard.nextLine(); System.out.print("Enter price >> "); price = keyboard.nextDouble(); keyboard.nextLine();
7. Finish the method by assigning the entered service and price to the SpaService object parameter using the SpaService class's setServiceDescription() and setPrice() methods. Then return the full object to the main() method, where it is assigned to the object used in the method call. Add a closing curly brace for the method.
s.setServiceDescription(service); s.setPrice(price); return s; }
8. Save the file CreateSpaServices.java, compile it, and execute it. The screenshot below shows a typical execution of the CreateSpaServices program that uses a data entry method. The execution is no different from the original version of the program, but by creating a method that accepts an unfilled SpaService object and returns one filled with data, you have made the main() method shorter and reused the data entry code.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started