-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2b9b700
commit f72427a
Showing
12 changed files
with
423 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
|
||
public class BankAccount { | ||
private String owner; | ||
private int balance; | ||
|
||
public BankAccount(String owner) { | ||
this(owner, 0); | ||
}//end ctor | ||
|
||
public BankAccount(String owner, int balance) { | ||
this.owner = owner; | ||
this.balance = balance; | ||
}//end ctor | ||
|
||
public void deposit(int amount) { | ||
if(amount > 0) { | ||
balance += amount; //balance = balance + amount; | ||
} | ||
else { | ||
System.out.println("Amount to deposit must be greater than 0"); | ||
}//end if-else | ||
}//end deposit | ||
|
||
public void withdraw(int amount) { | ||
if(amount > 0 && amount <= balance) { | ||
balance -= amount; //balance = balance - amount; | ||
} | ||
else { | ||
System.out.println("The amount to deposit must be greater than 0 " + | ||
"and less than your balance."); | ||
} | ||
}//end withdraw | ||
|
||
public String getOwner() { | ||
return owner; | ||
} | ||
|
||
public int getBalance() { | ||
return balance; | ||
} | ||
}//end BankAccount |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
public class BankAccountDemo { | ||
public static void main(String[] args) { | ||
BankAccount myAccount = new BankAccount("John Baugh", 5000); | ||
BankAccount bobsAccount = new BankAccount("Bob Robinson"); | ||
|
||
bobsAccount.deposit(500); | ||
System.out.println("owner: " + bobsAccount.getOwner()); | ||
System.out.println("blance: " + bobsAccount.getBalance()); | ||
|
||
bobsAccount.withdraw(1000); //warning! | ||
System.out.println("owner: " + bobsAccount.getOwner()); | ||
System.out.println("balance: " + bobsAccount.getBalance()); | ||
System.out.println(); | ||
|
||
System.out.println("owner: " + myAccount.getOwner()); | ||
System.out.println("balance: " + myAccount.getBalance()); | ||
System.out.println(); | ||
|
||
System.out.println("Deposit 1000?"); | ||
myAccount.deposit(1000); | ||
|
||
System.out.println("owner: " + myAccount.getOwner()); | ||
System.out.println("balance: " + myAccount.getBalance()); | ||
}//end main | ||
}//end BankAccountDemo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
|
||
public class Book { | ||
private String author; | ||
private String title; | ||
private String genre; | ||
private int numPages; | ||
|
||
public Book(String author, String title, String genre, int numPages) { | ||
this.author = author; | ||
this.title = title; | ||
this.genre = genre; | ||
this.numPages = numPages; | ||
}//end ctor | ||
|
||
public String getAuthor() { | ||
return author; | ||
} | ||
|
||
public String getTitle() { | ||
return title; | ||
} | ||
|
||
public String getGenre() { | ||
return genre; | ||
} | ||
|
||
public int getNumPages() { | ||
return numPages; | ||
} | ||
|
||
public void printBookDetails() { | ||
System.out.println(title); | ||
System.out.println("by " + author); | ||
System.out.println("has " + numPages + | ||
" pages, and its genre is " + genre); | ||
System.out.println(); | ||
}//end printBookDetails | ||
}//end Book class |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
|
||
public class BookDemo { | ||
public static void main(String[] args) { | ||
Book gameOfThrones = new Book("George Martin", | ||
"Game of Thrones", "Fantasy", 864); | ||
|
||
Book mathBook = new Book("James Stewart", | ||
"Calculus", "Math", 1392); | ||
|
||
Book javaBook = new Book("Joel Murach", | ||
"Murach's Java Programming", "Programming", 800); | ||
|
||
gameOfThrones.printBookDetails(); | ||
mathBook.printBookDetails(); | ||
javaBook.printBookDetails(); | ||
}//end main | ||
|
||
// public static void printBookDetails(Book book) { | ||
// System.out.println(book.getTitle()); | ||
// System.out.println("by " + book.getAuthor()); | ||
// System.out.println("has " + book.getNumPages() + | ||
// " pages, and its genre is " + book.getGenre()); | ||
// | ||
// System.out.println(); | ||
// }//end printBookDetails | ||
}//end BookDemo class |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
|
||
public class Circle { | ||
private double radius; | ||
|
||
public Circle() { | ||
this(1.0); | ||
}//no arg ctor | ||
|
||
public Circle(double radius) { | ||
this.radius = radius; | ||
}//end ctor | ||
|
||
public double getRadius() { | ||
return radius; | ||
}//end getRadius | ||
|
||
public void setRadius(double radius) { | ||
this.radius = radius; | ||
}//end setRadius | ||
|
||
public double circumference() { | ||
return 2 * Math.PI * radius; | ||
}//end circumference | ||
|
||
public double area() { | ||
return Math.PI * Math.pow(radius, 2); | ||
}//end area | ||
}//end Circle class | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
|
||
public class CircleDemo { | ||
public static void main(String[] args) { | ||
Circle unitCircle = new Circle(); | ||
Circle myCircle = new Circle(5); | ||
Circle yourCircle = new Circle(12.75); | ||
|
||
printCircleData(unitCircle); | ||
printCircleData(myCircle); | ||
printCircleData(yourCircle); | ||
}//end main | ||
|
||
public static void printCircleData(Circle circle) { | ||
System.out.println("r = " + circle.getRadius()); | ||
System.out.println("C = " + | ||
String.format("%.2f",circle.circumference()) | ||
); | ||
System.out.println("A = " + | ||
String.format("%.2f", circle.area()) | ||
); | ||
System.out.println(); | ||
}//end printCircleData | ||
}//end CircleDemo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
|
||
|
||
public class House { | ||
private int numStories; | ||
private int numWindows; | ||
private String color; | ||
|
||
//no-arg constructor | ||
public House() { | ||
numStories = 1; | ||
numWindows = 4; | ||
color = "gray"; | ||
}//end ctor | ||
|
||
//parameterized ctor | ||
public House(int numStories, int numWindows, String color) { | ||
this.numStories = numStories; | ||
this.numWindows = numWindows; | ||
this.color = color; | ||
}//end param ctor | ||
|
||
|
||
public int getNumStories() { | ||
return numStories; | ||
} | ||
|
||
public void setNumStories(int numStories) { | ||
this.numStories = numStories; | ||
|
||
numStories = numStories; | ||
} | ||
|
||
public int getNumWindows() { | ||
return numWindows; | ||
} | ||
|
||
public void setNumWindows(int numWindows) { | ||
this.numWindows = numWindows; | ||
} | ||
|
||
public String getColor() { | ||
return color; | ||
} | ||
|
||
public void setColor(String color) { | ||
this.color = color; | ||
} | ||
}//end House |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
public class HouseDemo { | ||
public static void main(String[] args) { | ||
House myHouse = new House(); | ||
House yourHouse = new House(3, 10, "blue"); | ||
|
||
myHouse.setNumStories(2); | ||
myHouse.setNumWindows(6); | ||
myHouse.setColor("red"); | ||
|
||
printHouseData(myHouse); | ||
System.out.println(); | ||
printHouseData(yourHouse); | ||
// yourHouse.setNumStories(3); | ||
// yourHouse.setNumWindows(10); | ||
// yourHouse.setColor("blue"); | ||
|
||
// System.out.println("My house is " + myHouse.getColor() + | ||
// " and has " + myHouse.getNumStories() + " stories and " + | ||
// myHouse.getNumWindows() + " windows."); | ||
// System.out.println(); | ||
// | ||
// System.out.println("Your house is " + yourHouse.getColor() + | ||
// " and has " + yourHouse.getNumStories() + " stories and " + | ||
// yourHouse.getNumWindows() + " windows."); | ||
}//end main | ||
|
||
public static void printHouseData(House house) { | ||
System.out.println("The house is " + house.getColor() + | ||
"\nand has " + house.getNumStories() + " stories and " + | ||
"\n" + house.getNumWindows() + " windows."); | ||
}//end printHouseData | ||
}//end HouseDemo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import java.util.ArrayList; | ||
|
||
public class IceCream { | ||
private String name; | ||
private int cost; | ||
private int numScoops; | ||
private ArrayList<String> toppings; | ||
|
||
public IceCream(String name, int cost, int numScoops) { | ||
this.name = name; | ||
this.cost = cost; | ||
this.numScoops = numScoops; | ||
this.toppings = new ArrayList<>(); | ||
}//end IceCream ctor | ||
|
||
public void addTopping(String topping) { | ||
toppings.add(topping); | ||
}//end addTopping | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public int getCost() { | ||
return cost; | ||
} | ||
|
||
public int getNumScoops() { | ||
return numScoops; | ||
} | ||
|
||
public void printToppings() { | ||
for(String topping : toppings) { | ||
System.out.println("\t" + topping); | ||
}//end for | ||
}//end printToppings | ||
}//end class | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
public class IceCreamDemo { | ||
public static void main(String[] args) { | ||
IceCream myIC = new IceCream("Chocolate", 3, 2); | ||
IceCream yourIC = new IceCream("Strawberry", 2, 1); | ||
|
||
myIC.addTopping("nuts"); | ||
myIC.addTopping("cherries"); | ||
|
||
yourIC.addTopping("sprinkles"); | ||
|
||
printIC(myIC); | ||
printIC(yourIC); | ||
|
||
}//end main | ||
|
||
public static void printIC(IceCream icecream) { | ||
System.out.println(icecream.getName()); | ||
System.out.println("Cost: $" + icecream.getCost()); | ||
System.out.println("Topped with:"); | ||
icecream.printToppings(); | ||
System.out.println(); //extra space | ||
}//end printIC | ||
}//end IceCreamDemo |
Oops, something went wrong.