Question: 3 . 5 ( Keyword new ) What s the purpose of keyword new? Explain what happens when you use it . 3 . 6

3.5(Keyword new) Whats the purpose of keyword new? Explain what happens when you use it.
3.6(Default Constructors) What is a default constructor? How are an objects instance variables initialized if a class has only a default constructor?
3.7(Instance Variables) Explain the purpose of an instance variable.
3.8(Using Classes without Importing Them) Most classes need to be imported before they can be used in an app. Why is every app allowed to use classes System and String without first importing them?
3.9(Using a Class without Importing It) Explain how a program could use class Scanner without importing it.
3.10(set and get Methods) Explain why a class might provide a set method and a get method for an instance variable.
3.11(Modified Account Class) Modify class Account (Fig.3.8) to provide a method called withdraw that withdraws money from an Account. Ensure that the withdrawal amount does not exceed the Accounts balance. If it does, the balance should be left unchanged and the method should print a message indicating "Withdrawal amount exceeded account balance." Modify class AccountTest (Fig.3.9) to test method withdraw.
3.12(Invoice Class) Create a class called Invoice that a hardware store might use to represent an invoice for an item sold at the store. An Invoice should include four pieces of information as instance variablesa part number (type String), a part description (type String), a quantity of the item being purchased (type int) and a price per item (double). Your class should have a constructor that initializes the four instance variables. Provide a set and a get method for each instance variable. In addition, provide a method named getInvoiceAmount that calculates the invoice amount (i.e., multiplies the quantity by the price per item), then returns the amount as a double value. If the quantity is not positive, it should be set to 0. If the price per item is not positive, it should be set to 0.0. Write a test app named InvoiceTest that demonstrates class Invoices capabilities.
3.13(Employee Class) Create a class called Employee that includes three instance variablesa first name (type String), a last name (type String) and a monthly salary (double). Provide a constructor that initializes the three instance variables. Provide a set and a get method for each instance variable. If the monthly salary is not positive, do not set its value. Write a test app named EmployeeTest that demonstrates class Employees capabilities. Create two Employee objects and display each objects yearly salary. Then give each Employee a 10% raise and display each Employees yearly salary again.
3.14(Date Class) Create a class called Date that includes three instance variablesa month (type int), a day (type int) and a year (type int). Provide a constructor that initializes the three instance variables and assumes that the values provided are correct. Provide a set and a get method for each instance variable. Provide a method displayDate that displays the month, day and year separated by forward slashes (/). Write a test app named DateTest that demonstrates class Dates capabilities.
3.15(Removing Duplicated Code in Method main) In the AccountTest class of Fig. 3.9, method main contains six statements (lines 1112,1314,2627,2829,3839 and 4041) that each display an Account objects name and balance. Study these statements and youll notice that they differ only in the Account object being manipulatedaccount1 or account2. In this exercise, youll define a new displayAccount method that contains one copy of that output statement. The methods parameter will be an Account object and the method will output the objects name and balance. Youll then replace the six duplicated statements in main with calls to displayAccount, passing as an argument the specific Account object to output.
Modify class AccountTest of Fig. 3.9 to declare method displayAccount (Fig.3.20) after the closing right brace of main and before the closing right brace of class AccountTest. Replace the comment in the methods body with a statement that displays accountToDisplays name and balance.
Fig. 3.20
1 public static void displayAccount(Account accountToDisplay){
2// place the statement that displays
3// accountToDisplay's name and balance here
4}
Method displayAccount to add to class Account.
Recall that main is a static method, so it can be called without first creating an object of the class in which main is declared. We also declared method displayAccount as a static method. When main needs to call another method in the same class without first creating an object of that class, the other method also must be declared static.
Once youve completed displayAccounts declaration, modify main to replace the statements that display each Accounts name and balance with calls to displayAccounteach receiving as its argument the account1 or account2 object, as appropr

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!