Question: In the previous chapter, Self-Test Question 16 described a class Person to represent a person. The class has instance variables for a persons name, which

In the previous chapter, Self-Test Question 16 described a class Person to represent a person. The class has instance variables for a person’s name, which is a string, and an integer age. These variables are name and age, respectively.

a. Write a default constructor for Person that sets name to the string "No name yet" and age to zero.

b. Write a second constructor for Person that sets name to a given string and age to a given age.

c. Write a static method createAdult for Person that returns a special instance of this class. The instance represents a generic adult and has the name "An adult" and the age 21.


Self-Test Question 16

Is the following valid, given the class SavingsAccount in Listing 6.7?
SavingsAccount.addInterest();


import java.util.Scanner; /** Class with static and nonstatic members. */ public class SavingsAccount { private double balance; public static double interestRate = 0; public static int number0fAccounts public SavingsAccount () An instance variable (nonstatic) Static variables = 0; { balance 0; A nonstatic method can number0fAccounts++; reference a static variable.import java.util.Scanner; /** Class with static and nonstatic members. */ public class

import java.util.Scanner; /** Class with static and nonstatic members. */ public class SavingsAccount { private double balance; public static double interestRate = 0; public static int number0fAccounts public SavingsAccount () An instance variable (nonstatic) Static variables = 0; { balance 0; A nonstatic method can number0fAccounts++; reference a static variable. public static void setInterestRate (double newRate) { interestRate = newRate; } A static method can reference a static variable but not an instance variable. public static double getInterestRate () { return interestRate; public static int getNumber0fAccounts () { return numberOfAccounts; public void deposit (double amount) { balance = balance + amount; public double withdraw (double amount) { if (balance >= amount) balance = balance - amount; else amount = 0; return amount; }

Step by Step Solution

3.52 Rating (166 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

public class Person private String name private int age Creates a new instance of Person public Person name No name yet age 0 public PersonString aNam... View full answer

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 Java An Introduction to Problem Solving and Progra Questions!