From f72427a3a94f6e198be4571e051429b085d532c6 Mon Sep 17 00:00:00 2001 From: "John P. Baugh" Date: Wed, 26 Jan 2022 23:42:48 -0500 Subject: [PATCH] Add files via upload --- section7/BankAccount.java | 41 +++++++++++++++++++++++++++ section7/BankAccountDemo.java | 25 +++++++++++++++++ section7/Book.java | 38 +++++++++++++++++++++++++ section7/BookDemo.java | 26 ++++++++++++++++++ section7/Circle.java | 44 +++++++++++++++++++++++++++++ section7/CircleDemo.java | 23 ++++++++++++++++ section7/House.java | 48 ++++++++++++++++++++++++++++++++ section7/HouseDemo.java | 32 +++++++++++++++++++++ section7/IceCream.java | 52 +++++++++++++++++++++++++++++++++++ section7/IceCreamDemo.java | 23 ++++++++++++++++ section7/Rectangle.java | 46 +++++++++++++++++++++++++++++++ section7/RectangleDemo.java | 25 +++++++++++++++++ 12 files changed, 423 insertions(+) create mode 100644 section7/BankAccount.java create mode 100644 section7/BankAccountDemo.java create mode 100644 section7/Book.java create mode 100644 section7/BookDemo.java create mode 100644 section7/Circle.java create mode 100644 section7/CircleDemo.java create mode 100644 section7/House.java create mode 100644 section7/HouseDemo.java create mode 100644 section7/IceCream.java create mode 100644 section7/IceCreamDemo.java create mode 100644 section7/Rectangle.java create mode 100644 section7/RectangleDemo.java diff --git a/section7/BankAccount.java b/section7/BankAccount.java new file mode 100644 index 0000000..6cc2662 --- /dev/null +++ b/section7/BankAccount.java @@ -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 diff --git a/section7/BankAccountDemo.java b/section7/BankAccountDemo.java new file mode 100644 index 0000000..74b66aa --- /dev/null +++ b/section7/BankAccountDemo.java @@ -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 diff --git a/section7/Book.java b/section7/Book.java new file mode 100644 index 0000000..963a901 --- /dev/null +++ b/section7/Book.java @@ -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 diff --git a/section7/BookDemo.java b/section7/BookDemo.java new file mode 100644 index 0000000..a3e2617 --- /dev/null +++ b/section7/BookDemo.java @@ -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 diff --git a/section7/Circle.java b/section7/Circle.java new file mode 100644 index 0000000..fc6b1dd --- /dev/null +++ b/section7/Circle.java @@ -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 + + + + + + + + + + + + + + + + diff --git a/section7/CircleDemo.java b/section7/CircleDemo.java new file mode 100644 index 0000000..7be97aa --- /dev/null +++ b/section7/CircleDemo.java @@ -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 diff --git a/section7/House.java b/section7/House.java new file mode 100644 index 0000000..9acf776 --- /dev/null +++ b/section7/House.java @@ -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 diff --git a/section7/HouseDemo.java b/section7/HouseDemo.java new file mode 100644 index 0000000..aaaf616 --- /dev/null +++ b/section7/HouseDemo.java @@ -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 diff --git a/section7/IceCream.java b/section7/IceCream.java new file mode 100644 index 0000000..12dff1d --- /dev/null +++ b/section7/IceCream.java @@ -0,0 +1,52 @@ +import java.util.ArrayList; + +public class IceCream { + private String name; + private int cost; + private int numScoops; + private ArrayList 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 + + + + + + + + + + + + + + + diff --git a/section7/IceCreamDemo.java b/section7/IceCreamDemo.java new file mode 100644 index 0000000..bbb9cc0 --- /dev/null +++ b/section7/IceCreamDemo.java @@ -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 diff --git a/section7/Rectangle.java b/section7/Rectangle.java new file mode 100644 index 0000000..e8abd1a --- /dev/null +++ b/section7/Rectangle.java @@ -0,0 +1,46 @@ + +public class Rectangle { + private double length; + private double width; + + private static int numRectangles; + + public Rectangle() { + this(1.0, 1.0); + }//end no-arg ctor + + public Rectangle(double length, double width) { + this.length = length; + this.width = width; + numRectangles++; + }//end ctor + + public double getLength() { + return length; + } + + public double getWidth() { + return width; + } + + public void setLength(double length) { + this.length = length; + } + + public void setWidth(double width) { + this.width = width; + } + + public double area() { + return length * width; + } + + public double perimeter() { + return 2 * length + 2 * width; + } + + public static int getNumRectangles() { + return numRectangles; + } + +} diff --git a/section7/RectangleDemo.java b/section7/RectangleDemo.java new file mode 100644 index 0000000..a2b7a3a --- /dev/null +++ b/section7/RectangleDemo.java @@ -0,0 +1,25 @@ +import org.w3c.dom.css.Rect; + +public class RectangleDemo { + public static void main(String[] args) { + Rectangle r1 = new Rectangle(); + Rectangle r2 = new Rectangle(5, 10); + + System.out.println(Rectangle.getNumRectangles()); + + Rectangle r3; + + System.out.println("After r3 declared: " + Rectangle.getNumRectangles()); + + r3 = new Rectangle(2.5, 4); + + System.out.println("After r3 instantiated: " + Rectangle.getNumRectangles()); + + System.out.println(r1.area()); + System.out.println(r2.area()); + System.out.println(r3.area()); + + System.out.println(r1.perimeter()); + + }//end main +}