8.Open the class ”Account”. Create getter and setter methods for setting and ret
ID: 3598715 • Letter: 8
Question
8.Open the class ”Account”. Create getter and setter methods for setting and retrieving the values of the instance variables name and balance.
9.In class ”Account”, look at the method printNum- bers(int limit). Add a for loop to this method. The for loop should count from 0 to limit-1. For each iteration, you should print the current iteration using System.out.println(variableUsedForInitialValueInForLoop)
10.To compare Strings, remember that you need to call the following method: nameOfSomeString.equals(”String you want to compare”). For example:
String a = ”Hello”;
boolean b = a.equals(”Hello”);
This checks, for string a of value ”Hello”, if the string given to it is also ”Hello”. In class ”Account”, write a method: public void compareToName(String nameToCompare) {}
That prints a message of success if the String given as an argument is equal to the instance variable name, or a failure message otherwise.
Explanation / Answer
Here is the Account class after adding the code for the given questions
Accoutn.java
import java.util.Scanner;
public class Account {
private double balance;
private String name;
//Question 8: add the getters and setter for balance and name here:
//getters
//method that gets the balance
public double getBalance()
{
return balance;
}
//method that gets name
public String getName()
{
return name;
}
//setters
//method that sets balance
public void setBalance(double dblbalance)
{
balance = dblbalance;
}
//method that sets name
public void setName(String strName)
{
name = strName;
}
//Question 9: add the for loop to the following method:
public void printNumbers(int limit)
{
//Your for loop should go here
for(int number = 0; number <= limit-1; number++)
{
System.out.println(number);
}
}
//Question 10: put your method for comparing a String to name here
public void compareToName(String nameToCompare)
{
//check the names
if(name.equals(nameToCompare))
//print success message if the names are qual
System.out.println("Both the name are equal");
else
//else print failure message
System.out.println("Both the name are not equal");
}
}
Let me know if you have any concerns
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.