forked from angiejones/java-programming
-
Notifications
You must be signed in to change notification settings - Fork 0
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
62e7026
commit 6dc4eab
Showing
67 changed files
with
2,232 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,8 @@ | ||
package chapter10; | ||
|
||
public class Animal { | ||
|
||
public void makeSound(){ | ||
System.out.println("unknown animal sound"); | ||
} | ||
} |
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,14 @@ | ||
package chapter10; | ||
|
||
public class Cat extends Animal { | ||
|
||
@Override | ||
public void makeSound(){ | ||
System.out.println("meow"); | ||
} | ||
|
||
public void scratch(){ | ||
System.out.println("I am a cat. I scratch things."); | ||
} | ||
|
||
} |
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,13 @@ | ||
package chapter10; | ||
|
||
public class Dog extends Animal { | ||
|
||
@Override | ||
public void makeSound(){ | ||
System.out.println("woof"); | ||
} | ||
|
||
public void fetch(){ | ||
System.out.println("fetch is fun!"); | ||
} | ||
} |
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 @@ | ||
package chapter10; | ||
|
||
public class Zoo { | ||
|
||
public static void main(String[] args){ | ||
|
||
Dog rocky = new Dog(); | ||
rocky.fetch(); | ||
rocky.makeSound(); | ||
feed(rocky); | ||
|
||
Animal sasha = new Dog(); | ||
sasha.makeSound(); | ||
feed(sasha); | ||
|
||
sasha = new Cat(); | ||
sasha.makeSound(); | ||
((Cat) sasha).scratch(); | ||
feed(sasha); | ||
} | ||
|
||
public static void feed(Animal animal){ | ||
|
||
if(animal instanceof Dog){ | ||
System.out.println("here's your dog food"); | ||
} | ||
|
||
else if(animal instanceof Cat){ | ||
System.out.println("here's your cat food"); | ||
} | ||
} | ||
} |
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,65 @@ | ||
package chapter11; | ||
|
||
public class Book implements Product { | ||
|
||
private double price; | ||
private String name; | ||
private String color; | ||
private String author; | ||
private int pages; | ||
private String isbn; | ||
|
||
@Override | ||
public double getPrice() { | ||
return price; | ||
} | ||
|
||
@Override | ||
public void setPrice(double price) { | ||
this.price = price; | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return name; | ||
} | ||
|
||
@Override | ||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
@Override | ||
public String getColor() { | ||
return color; | ||
} | ||
|
||
@Override | ||
public void setColor(String color) { | ||
this.color = color; | ||
} | ||
|
||
public String getAuthor() { | ||
return author; | ||
} | ||
|
||
public void setAuthor(String author) { | ||
this.author = author; | ||
} | ||
|
||
public int getPages() { | ||
return pages; | ||
} | ||
|
||
public void setPages(int pages) { | ||
this.pages = pages; | ||
} | ||
|
||
public String getIsbn() { | ||
return isbn; | ||
} | ||
|
||
public void setIsbn(String isbn) { | ||
this.isbn = isbn; | ||
} | ||
} |
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,10 @@ | ||
package chapter11; | ||
|
||
public class Customer { | ||
|
||
public static void main(String[] args){ | ||
|
||
Product book = new Book(); | ||
book.setPrice(9.99); | ||
} | ||
} |
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,17 @@ | ||
package chapter11; | ||
|
||
public interface Product { | ||
|
||
double getPrice(); | ||
void setPrice(double price); | ||
|
||
String getName(); | ||
void setName(String name); | ||
|
||
String getColor(); | ||
void setColor(String color); | ||
|
||
default String getBarcode(){ | ||
return "no barcode"; | ||
} | ||
} |
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,33 @@ | ||
package chapter11; | ||
|
||
public class Rectangle extends Shape { | ||
|
||
private double length; | ||
private double width; | ||
|
||
public double getLength() { | ||
return length; | ||
} | ||
|
||
public void setLength(double length) { | ||
this.length = length; | ||
} | ||
|
||
public double getWidth() { | ||
return width; | ||
} | ||
|
||
public void setWidth(double width) { | ||
this.width = width; | ||
} | ||
|
||
public Rectangle(double length, double width){ | ||
setLength(length); | ||
setWidth(width); | ||
} | ||
|
||
@Override | ||
double calculateArea() { | ||
return length * width; | ||
} | ||
} |
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,10 @@ | ||
package chapter11; | ||
|
||
public abstract class Shape { | ||
|
||
abstract double calculateArea(); | ||
|
||
public void print(){ | ||
System.out.println("I am a shape"); | ||
} | ||
} |
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,11 @@ | ||
package chapter11; | ||
|
||
public class ShapeTester { | ||
|
||
public static void main(String[] args){ | ||
|
||
Shape rectangle = new Rectangle(5, 7); | ||
rectangle.print(); | ||
System.out.println(rectangle.calculateArea()); | ||
} | ||
} |
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,96 @@ | ||
package chapter12; | ||
|
||
import java.util.*; | ||
|
||
public class CollectionsDemo { | ||
|
||
public static void main(String[] args){ | ||
setDemo(); | ||
listDemo(); | ||
queueDemo(); | ||
mapDemo(); | ||
} | ||
|
||
public static void setDemo(){ | ||
Set fruit = new HashSet(); | ||
fruit.add("apple"); | ||
fruit.add("lemon"); | ||
fruit.add("banana"); | ||
fruit.add("orange"); | ||
fruit.add("lemon"); | ||
|
||
System.out.println(fruit.size()); //4 | ||
System.out.println(fruit); //[banana, orange, apple, lemon] | ||
} | ||
|
||
public static void listDemo(){ | ||
List fruit = new ArrayList(); | ||
fruit.add("apple"); | ||
fruit.add("lemon"); | ||
fruit.add("banana"); | ||
fruit.add("orange"); | ||
fruit.add("lemon"); | ||
|
||
System.out.println(fruit.get(2)); //banana | ||
System.out.println(fruit.size()); //5 | ||
System.out.println(fruit); //[apple, lemon, banana, orange, lemon] | ||
} | ||
|
||
public static void queueDemo(){ | ||
Queue fruit = new LinkedList(); | ||
fruit.add("apple"); | ||
fruit.add("lemon"); | ||
fruit.add("banana"); | ||
fruit.add("orange"); | ||
fruit.add("lemon"); | ||
|
||
System.out.println(fruit.size()); //5 | ||
System.out.println(fruit); //[apple, lemon, banana, orange, lemon] | ||
|
||
fruit.remove(); | ||
System.out.println(fruit);//[lemon, banana, orange, lemon] | ||
|
||
System.out.println(fruit.peek());//lemon | ||
} | ||
|
||
public static void mapDemo(){ | ||
Map fruitCalories = new HashMap(); | ||
fruitCalories.put("apple", 95); | ||
fruitCalories.put("lemon", 20); | ||
fruitCalories.put("banana", 105); | ||
fruitCalories.put("orange", 45); | ||
fruitCalories.put("lemon", 17); | ||
|
||
System.out.println(fruitCalories.size()); //4 | ||
System.out.println(fruitCalories); //{banana=105, orange=45, apple=95, lemon=17} | ||
|
||
System.out.println(fruitCalories.get("lemon"));//17 | ||
|
||
System.out.println(fruitCalories.entrySet());//[banana=105, orange=45, apple=95, lemon=17] | ||
|
||
fruitCalories.remove("orange"); | ||
System.out.println(fruitCalories);//{banana=105, apple=95, lemon=17} | ||
} | ||
|
||
public void print(Collection<String> collection){ | ||
var i = collection.iterator(); | ||
while(i.hasNext()){ | ||
System.out.println(i.next()); | ||
} | ||
|
||
for(String item : collection){ | ||
System.out.println(item); | ||
} | ||
|
||
collection.forEach(System.out::println); | ||
} | ||
|
||
public void print(Map<String, Integer> map){ | ||
for(var entry : map.entrySet()){ | ||
System.out.println(entry.getValue()); | ||
} | ||
|
||
map.forEach( | ||
(k,v)->System.out.println("Fruit: " + k + ", Calories: " + v)); | ||
} | ||
} |
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,50 @@ | ||
package chapter13; | ||
|
||
|
||
import java.io.File; | ||
import java.io.FileNotFoundException; | ||
import java.io.IOException; | ||
import java.util.InputMismatchException; | ||
import java.util.Scanner; | ||
|
||
public class ExceptionHandling { | ||
|
||
public static void main(String args[]){ | ||
createNewFile(); | ||
numbersExceptionHandling(); | ||
} | ||
|
||
public static void createNewFile(){ | ||
File file = new File("resources/nonexistent.txt"); | ||
try{ | ||
file.createNewFile(); | ||
}catch (Exception e){ | ||
System.out.println("Directory does not exist."); | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
public static void createNewFileRethrow() throws IOException{ | ||
File file = new File("resources/nonexistent.txt"); | ||
file.createNewFile(); | ||
} | ||
|
||
public static void numbersExceptionHandling(){ | ||
File file = new File("resources/numbers.txt"); | ||
Scanner fileReader = null; | ||
try{ | ||
fileReader = new Scanner(file); | ||
|
||
while(fileReader.hasNext()){ | ||
double num = fileReader.nextDouble(); | ||
System.out.println(num); | ||
} | ||
|
||
|
||
}catch(FileNotFoundException | InputMismatchException e){ | ||
e.printStackTrace(); | ||
}finally{ | ||
fileReader.close(); | ||
} | ||
} | ||
} |
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 @@ | ||
package chapter2; | ||
|
||
import java.util.Scanner; | ||
|
||
public class GrossPayCalculator { | ||
|
||
public static void main(String arg[]){ | ||
|
||
//1. Get the number of hours worked | ||
System.out.println("Enter the number of hours the employee worked."); | ||
Scanner scanner = new Scanner(System.in); | ||
int hours = scanner.nextInt(); | ||
|
||
//2. Get the hourly pay rate | ||
System.out.println("Enter the employee's pay rate."); | ||
double rate = scanner.nextDouble(); | ||
scanner.close(); | ||
|
||
//3. Multiply hours and pay rate | ||
double grossPay = hours * rate; | ||
|
||
//4. Display result | ||
System.out.println("The employee's gross pay is $" + grossPay); | ||
} | ||
|
||
} |
Oops, something went wrong.