Question
I will like a review of the answers to the questions i have provided below . please help look at the answers if they are
I will like a review of the answers to the questions i have provided below. please help look at the answers if they are correct. if they are not, please provide correct solutions. thanks
1. Assume a class has the following instance variable:
private String name;
Write the set and get methods to set and get the instance variable value.
Here is the answer i have:
public class Account {
private String name; // instance variable
// method to set the name in the object
public void setName (String name) {
this.name = name; // store the name
}
// method to retrieve the name from the object
public String getName() {
return name; // return value of name to caller
}
}
second question
2. If the class Person has the instance variable String name, write the class's constructor that will initialize the name instance variable using a parameter called name.
Answer
public class Account {
private String name; // instance variable
// constructor initializes name with parameter name
public Account (String name) { // constructor name is class name
this.name = name;
}
//method to set the name
public void setName(String name) {
this.name = name;
}
// method to retrieve the name
public String getName() {
return name;
}
}
third question
3. Write the Java code to instantiate an Account object from an Account class that has a default constructor
Answer
public class AccountTest {
public static void main(String[] args) {
// create two Account objects
Account account1 = new Account("Jane Green");
Account account2 = new Account("John Blue");
//display initital value of name for each Account
System.out.printf("account1 name is: %s%n", account1.getNme());
System.out.printf("account2 name is : %s%n", account2.getName());
}
}
please help look at the answers if they are correct. if they are not, please provide correct solutions. thanks
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