Skip to content

Commit

Permalink
initial checkin
Browse files Browse the repository at this point in the history
  • Loading branch information
angiejones committed Apr 21, 2019
1 parent 62e7026 commit 6dc4eab
Show file tree
Hide file tree
Showing 67 changed files with 2,232 additions and 0 deletions.
8 changes: 8 additions & 0 deletions chapter10/Animal.java
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");
}
}
14 changes: 14 additions & 0 deletions chapter10/Cat.java
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.");
}

}
13 changes: 13 additions & 0 deletions chapter10/Dog.java
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!");
}
}
32 changes: 32 additions & 0 deletions chapter10/Zoo.java
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");
}
}
}
65 changes: 65 additions & 0 deletions chapter11/Book.java
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;
}
}
10 changes: 10 additions & 0 deletions chapter11/Customer.java
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);
}
}
17 changes: 17 additions & 0 deletions chapter11/Product.java
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";
}
}
33 changes: 33 additions & 0 deletions chapter11/Rectangle.java
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;
}
}
10 changes: 10 additions & 0 deletions chapter11/Shape.java
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");
}
}
11 changes: 11 additions & 0 deletions chapter11/ShapeTester.java
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());
}
}
96 changes: 96 additions & 0 deletions chapter12/CollectionsDemo.java
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));
}
}
50 changes: 50 additions & 0 deletions chapter13/ExceptionHandling.java
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();
}
}
}
26 changes: 26 additions & 0 deletions chapter2/GrossPayCalculator.java
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);
}

}
Loading

0 comments on commit 6dc4eab

Please sign in to comment.