- Introduction
- Defining Methods
- Calling Methods
- Scope
- Adding Parameters
- Reassigning Instance Fields
- Returns
- The toString() Method
- Review
public class SavingsAccount {
int balance;
public SavingsAccount(int initialBalance){
balance = initialBalance;
}
public static void main(String[] args){
SavingsAccount savings = new SavingsAccount(2000);
//Check balance:
System.out.println("Hello!");
System.out.println("Your balance is "+savings.balance);
//Withdrawing:
int afterWithdraw = savings.balance - 300;
savings.balance = afterWithdraw;
System.out.println("You just withdrew "+300);
//Check balance:
System.out.println("Hello!");
System.out.println("Your balance is "+savings.balance);
//Deposit:
int afterDeposit = savings.balance + 600;
savings.balance = afterDeposit;
System.out.println("You just deposited "+600);
//Check balance:
System.out.println("Hello!");
System.out.println("Your balance is "+savings.balance);
//Deposit:
int afterDeposit2 = savings.balance + 600;
savings.balance = afterDeposit2;
System.out.println("You just deposited "+600);
//Check balance:
System.out.println("Hello!");
System.out.println("Your balance is "+savings.balance);
}
}
-
We have made a SavingsAccount class without using any methods beyond
main()
and the constructor,SavingsAccount()
.Run the code to see some account behavior happen.
Look at the main method! It’s so long! There is so much repeated code! Can you imagine how messy it would look if you needed to make 10 deposits?
Throughout this lesson, we will learn how to make methods that would make checking the balance, depositing, and withdrawing all behavior that would take only one line of code.
public class Store {
// instance fields
String productType;
// constructor method
public Store(String product) {
productType = product;
}
// advertise method
// main method
public static void main(String[] args) {
}
}
-
In between the constructor and the
main()
method, add a method calledadvertise()
to theStore
class. It should be accessible by other classes, and should have no output.You can leave the body of the method empty.
-
Inside the
advertise()
method, type two print statements. They should result in the printouts:
"Come spend some money!"
"Selling productType!"
-
where
productType
is replaced with the value in the variableproductType
.However, we’re not going to see these Strings printed out yet! We’ll see in the next exercise how we can make these printouts actually run.
public class Store {
// instance fields
String productType;
// constructor method
public Store(String product) {
productType = product;
}
// advertise method
public void advertise() {
System.out.println("Selling " + productType + "!");
System.out.println("Come spend some money!");
}
// main method
public static void main(String[] args) {
Store lemonadeStand = new Store("Lemonade");
}
}
-
Last exercise, we defined a new method,
advertise()
, but we didn’t actually see it run.We now have a Store class with
advertise()
defined.Call the
advertise()
method on thelemonadeStand
object in themain()
method and see what the output is! -
Now, call the
advertise()
method on thelemonadeStand
object two more times. It should be called in themain()
method three times total.
public class Store {
// instance fields
String productType;
// constructor method
public Store(String product) {
productType = product;
}
// advertise method
public void advertise() {
String message = "Selling " + productType + "!";
System.out.println(message);
}
// main method
public static void main(String[] args) {
String cookie = "Cookies";
Store cookieShop = new Store(cookie);
cookieShop.advertise();
}
}
- Inside of the
advertise()
method, change theproductType
variable to thecookie
variable, which is declared in themain()
method. This should also result in the printout:
Selling cookies!
- Right?
- No! We got an error! The
cookie
variable cannot be accessed inside of the advertise method. The scope is wrong! Change it back toproductType
:
String message = "Selling " + productType + "!";
- Inside of the
main()
method, print theString
message
, which is declared in theadvertise()
method. This should print:
Selling Cookies!
- Right?
-
Foiled again! The
message
variable only exists inside the scope of theadvertise()
method!Delete the faulty print statement from the
main()
method.
public class Store {
// instance fields
String productType;
// constructor method
public Store(String product) {
productType = product;
}
// advertise method
public void advertise() {
String message = "Selling " + productType + "!";
System.out.println(message);
}
// main method
public static void main(String[] args) {
Store lemonadeStand = new Store("Lemonade");
}
}
-
Add a method to the Store class called
greetCustomer()
. It should be accessible by other classes, and return no output. For now, have it take no parameters and leave the body of the method empty. -
Modify the
greetCustomer()
method so that it accepts aString
parameter calledcustomer
. -
Inside of the
greetCustomer()
method, add a print statement to print:
"Welcome to the store, " + customer + "!"
- Inside the
main()
method, call thegreetCustomer()
method on thelemonadeStand
object. Pass in aString
argument of your choice!
public class Store {
// instance fields
String productType;
double price;
// constructor method
public Store(String product, double initialPrice) {
productType = product;
price = initialPrice;
}
// increase price method
public void increasePrice(double priceToAdd){
}
// main method
public static void main(String[] args) {
Store lemonadeStand = new Store("Lemonade", 3.75);
}
}
-
We have added a
price
instance field to the Store class.However, to combat inflation costs, we’ve found ourselves increasing the price of our product over and over. We’ve added an empty
increasePrice()
method to the Store class. It takes adouble
parameterpriceToAdd
.Inside of the
increasePrice()
method, create a variable callednewPrice
. Declare it to be adouble
, and set it equal to theprice
plus thepriceToAdd
. -
Inside of
increasePrice()
, set the instance fieldprice
to benewPrice
! -
In the
main()
method, increase the price at the lemonade stand by1.5
. Then, print thelemonadeStand.price
to see how it has changed!
public class Store {
// instance fields
String productType;
double price;
// constructor method
public Store(String product, double initialPrice) {
productType = product;
price = initialPrice;
}
// increase price method
public void increasePrice(double priceToAdd){
double newPrice = price + priceToAdd;
price = newPrice;
}
// get price with tax method
// main method
public static void main(String[] args) {
Store lemonadeStand = new Store("Lemonade", 3.75);
}
}
-
We want to have a method that returns the price plus tax.
Define a method called
getPriceWithTax()
that is intended to return the price plus the tax. It should take in no parameters and return adouble
.You can leave the body of the method empty for now. Note: the code will have an error until we return the correct type from the method, which we will do in the next step.
-
Inside the
getPriceWithTax()
method, create adouble
variabletotalPrice
that is equal toprice + price * 0.08
.0.08
is the tax applied to the price.Then, return
totalPrice
. -
Inside of
main()
, set adouble
variablelemonadePrice
to the value returned bylemonadeStand.getPriceWithTax()
.Now, print out
lemonadePrice
.
public class Store {
// instance fields
String productType;
double price;
// constructor method
public Store(String product, double initialPrice) {
productType = product;
price = initialPrice;
}
// increase price method
public void increasePrice(double priceToAdd){
double newPrice = price + priceToAdd;
price = newPrice;
}
// get price with tax method
public double getPriceWithTax(){
double tax = 0.08;
double totalPrice = price + price*tax;
return totalPrice;
}
// main method
public static void main(String[] args) {
Store lemonadeStand = new Store("Lemonade", 3.75);
Store cookieShop = new Store("Cookies", 5);
}
}
-
In the
main()
method, print the ObjectslemonadeStand
andcookieShop
. Are these printouts helpful in understanding these Objects? -
Create a
toString()
method for theStore
class. The method signature should say that it ispublic
, and that it returns aString
. It shouldn’t take in any parameters. For now, have it return theString
Store
. -
Store
isn’t very helpful! What kind of Store is it?Change the
toString()
to return aString
that describes thisStore
object.Your
String
should look like:
This store sells <productType> at a price of <price>.
- where
productType
andprice
are the values in those instance fields. For example, if it was a hat store where hats cost 8, theString
would say:
This store sells hats at a price of 8.
- Look at the printouts again. Are they more helpful now?
public class SavingsAccount {
int balance;
public SavingsAccount(int initialBalance){
balance = initialBalance;
}
public static void main(String[] args){
SavingsAccount savings = new SavingsAccount(2000);
//Check balance:
System.out.println("Hello!");
System.out.println("Your balance is "+savings.balance);
//Withdrawing:
int afterWithdraw = savings.balance - 300;
savings.balance = afterWithdraw;
System.out.println("You just withdrew "+300);
//Check balance:
System.out.println("Hello!");
System.out.println("Your balance is "+savings.balance);
//Deposit:
int afterDeposit = savings.balance + 600;
savings.balance = afterDeposit;
System.out.println("You just deposited "+600);
//Check balance:
System.out.println("Hello!");
System.out.println("Your balance is "+savings.balance);
//Deposit:
int afterDeposit2 = savings.balance + 600;
savings.balance = afterDeposit2;
System.out.println("You just deposited "+600);
//Check balance:
System.out.println("Hello!");
System.out.println("Your balance is "+savings.balance);
}
}
-
Now that we’ve learned about behavior, we can apply behavior to our
SavingsAccount
class using methods!We’ve added the functionality for each method inside
main()
now, but you will be rebuilding each abovemain()
. Note that your methods can directly access thebalance
field.First, write a method called
checkBalance()
that prints:
Hello!
Your balance is
-
with the balance of the account displayed.
-
It should take in no parameters and return nothing.
-
Now, write a method called
deposit()
that takes in anint
parameteramountToDeposit
and adds it to the balance. It should return nothing.If you want, you can also have the method print:
You just deposited amountToDeposit
with the value of `amountToDeposit` displayed.
-
Now, write a method called
withdraw()
that takes in anint
parameteramountToWithdraw
and subtracts it from the balance. It should return theamountToWithdraw
.If you want, you can also have the method print:
You just withdrew amountToWithdraw
- with the value of
amountToWithdraw
displayed.
-
Test out your methods by trying to replace some of the code in the
main()
method with the equivalent methods!Make sure to use
checkBalance()
,deposit()
, andwithdraw()
at least once each. -
Congratulations! You’ve made a basic SavingsAccount.
If you want, you can add more functionality to this! What other instance fields might you want to keep track of? What might a
toString()
look like for this class?