-
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
Showing
16 changed files
with
643 additions
and
1 deletion.
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
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,20 @@ | ||
package me.alex4386.gachon.sw14462.day25; | ||
|
||
import me.alex4386.gachon.sw14462.utils.Chainloader; | ||
|
||
public class Main { | ||
public static String chainloadTarget = "ex14_11"; | ||
|
||
public static void main(String[] args) throws Throwable { | ||
String packageName = Main.class.getPackage().getName(); | ||
String chainLoadTargetClass = packageName + "." + chainloadTarget + ".Main"; | ||
|
||
try { | ||
Chainloader.chainloadTarget(chainLoadTargetClass, args); | ||
} catch (Exception e) { | ||
throw e; | ||
} | ||
} | ||
|
||
} | ||
|
44 changes: 44 additions & 0 deletions
44
src/main/java/me/alex4386/gachon/sw14462/day25/ex14_1/Main.java
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 @@ | ||
package me.alex4386.gachon.sw14462.day25.ex14_1; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Scanner; | ||
|
||
public class Main { | ||
public static void main(String[] args) { | ||
Scanner scanner = new Scanner(System.in); | ||
ArrayList<Pet> pets = new ArrayList<>(); | ||
|
||
while (true) { | ||
System.out.print("Enter the name of the pet (type \"exit\" to exit): "); | ||
String name = scanner.nextLine(); | ||
if (name.equals("exit")) break; | ||
|
||
System.out.print("Enter the age of the pet: "); | ||
int age = scanner.nextInt(); | ||
scanner.nextLine(); | ||
|
||
System.out.print("Enter the weight of the pet: "); | ||
double weight = scanner.nextDouble(); | ||
|
||
Pet pet = new Pet(name, age, weight); | ||
pets.add(pet); | ||
|
||
scanner.nextLine(); | ||
} | ||
|
||
// sort by its name. | ||
for (int i = 0; i < pets.size(); i++) { | ||
for (int j = i + 1; j < pets.size(); j++) { | ||
if (pets.get(i).getName().compareTo(pets.get(j).getName()) > 0) { | ||
Pet temp = pets.get(i); | ||
pets.set(i, pets.get(j)); | ||
pets.set(j, temp); | ||
} | ||
} | ||
} | ||
|
||
for (Pet pet : pets) { | ||
pet.writeOutput(); | ||
} | ||
} | ||
} |
117 changes: 117 additions & 0 deletions
117
src/main/java/me/alex4386/gachon/sw14462/day25/ex14_1/Pet.java
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,117 @@ | ||
package me.alex4386.gachon.sw14462.day25.ex14_1; | ||
|
||
/** | ||
Class for basic pet data: name, age, and weight. */ | ||
public class Pet { | ||
private String name; | ||
private int age; // in years | ||
private double weight; // in pounds | ||
|
||
public Pet() // Default constructor | ||
{ | ||
name = "No name yet."; | ||
age = 0; | ||
weight = 0; | ||
} | ||
|
||
public Pet(String initialName, int initialAge, | ||
double initialWeight) { | ||
name = initialName; | ||
if ((initialAge < 0) || (initialWeight < 0)) { | ||
System.out.println("Error: Negative age or weight."); | ||
System.exit(0); | ||
} else { | ||
age = initialAge; | ||
weight = initialWeight; | ||
} | ||
} | ||
|
||
public void setPet(String newName, int newAge, | ||
double newWeight) { | ||
name = newName; | ||
if ((newAge < 0) || (newWeight < 0)) { | ||
System.out.println("Error: Negative age or weight."); | ||
System.exit(0); | ||
} else { | ||
age = newAge; | ||
weight = newWeight; | ||
} | ||
} | ||
|
||
public Pet(String initialName) { | ||
name = initialName; | ||
age = 0; | ||
weight = 0; | ||
} | ||
|
||
public void setName(String newName) { | ||
name = newName; //age and weight are unchanged. | ||
} | ||
|
||
|
||
|
||
|
||
public Pet(int initialAge) | ||
{ | ||
name = "No name yet."; | ||
weight = 0; | ||
if (initialAge < 0) | ||
{ | ||
System.out.println("Error: Negative age."); | ||
System.exit(0); | ||
} | ||
else | ||
age = initialAge; | ||
} | ||
public void setAge(int newAge) | ||
{ | ||
if (newAge < 0) | ||
{ | ||
System.out.println("Error: Negative age."); | ||
System.exit(0); | ||
} | ||
else | ||
age = newAge; | ||
//name and weight are unchanged. | ||
} | ||
|
||
public Pet(double initialWeight) | ||
{ | ||
name = "No name yet"; | ||
age = 0; | ||
if (initialWeight < 0) | ||
{ | ||
System.out.println("Error: Negative weight."); | ||
System.exit(0); | ||
}else weight = initialWeight; | ||
} | ||
|
||
public void setWeight(double newWeight) | ||
{ | ||
if (newWeight < 0) | ||
{ | ||
System.out.println("Error: Negative weight."); | ||
System.exit(0); | ||
} | ||
else | ||
weight = newWeight; //name and age are unchanged. | ||
} | ||
|
||
public String getName() | ||
{ | ||
return name; | ||
} | ||
public int getAge() | ||
{ | ||
return age; } | ||
public double getWeight() | ||
{ | ||
return weight; | ||
} | ||
public void writeOutput() { | ||
System.out.println("Name: " + name); | ||
System.out.println("Age: " + age + " years"); | ||
System.out.println("Weight: " + weight + " pounds"); | ||
} | ||
|
||
} |
58 changes: 58 additions & 0 deletions
58
src/main/java/me/alex4386/gachon/sw14462/day25/ex14_11/LinkedQueue.java
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,58 @@ | ||
package me.alex4386.gachon.sw14462.day25.ex14_11; | ||
|
||
import java.util.Iterator; | ||
import java.util.LinkedList; | ||
|
||
public class LinkedQueue<T extends Object> extends LinkedList<T> { | ||
|
||
private int front; | ||
private int count; | ||
|
||
public LinkedQueue() { | ||
super(); | ||
front = 0; | ||
count = 0; | ||
} | ||
|
||
public void addToQueue(T item) { | ||
this.add(item); | ||
this.count++; | ||
} | ||
|
||
public T removeFromQueue() { | ||
if (this.count == 0) { | ||
return null; | ||
} | ||
this.count--; | ||
return this.get(this.front++); | ||
} | ||
|
||
public boolean isEmpty() { | ||
return this.count == 0; | ||
} | ||
|
||
public Iterator<T> iterator() { | ||
return new Iterator<T>() { | ||
private int index = front; | ||
private int count = 0; | ||
|
||
@Override | ||
public boolean hasNext() { | ||
return count < LinkedQueue.this.count; | ||
} | ||
|
||
@Override | ||
public T next() { | ||
if (!this.hasNext()) return null; | ||
|
||
count++; | ||
return LinkedQueue.this.get(index++); | ||
} | ||
}; | ||
} | ||
|
||
@Override | ||
public int size() { | ||
return this.count; | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
src/main/java/me/alex4386/gachon/sw14462/day25/ex14_11/Main.java
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,56 @@ | ||
package me.alex4386.gachon.sw14462.day25.ex14_11; | ||
|
||
import java.util.Iterator; | ||
|
||
public class Main { | ||
public static void main(String[] args) { | ||
LinkedQueue<Integer> queue = new LinkedQueue<>(); | ||
|
||
for (int i = 0; i < 10; i++) { | ||
System.out.println("Pushing "+i+" to queue"); | ||
queue.addToQueue(i); | ||
|
||
System.out.println("Current Queue Content:"); | ||
printIterator(queue.iterator()); | ||
System.out.println(); | ||
} | ||
|
||
for (int i = 0; i < 5; i++) { | ||
int value = queue.removeFromQueue().intValue(); | ||
System.out.println("Popping "+value+" from queue"); | ||
|
||
System.out.println("Current Queue Content:"); | ||
printIterator(queue.iterator()); | ||
System.out.println(); | ||
} | ||
|
||
for (int i = 0; i < 5; i++) { | ||
System.out.println("Pushing "+i+" to queue"); | ||
queue.addToQueue(i); | ||
|
||
System.out.println("Current Queue Content:"); | ||
printIterator(queue.iterator()); | ||
System.out.println(); | ||
} | ||
|
||
|
||
while (!queue.isEmpty()) { | ||
int value = queue.removeFromQueue().intValue(); | ||
System.out.println("Popping "+value+" from queue"); | ||
|
||
System.out.println("Current Queue Content:"); | ||
printIterator(queue.iterator()); | ||
System.out.println(); | ||
} | ||
} | ||
|
||
public static<T extends Object> void printIterator(Iterator<T> iter) { | ||
boolean first = true; | ||
while (iter.hasNext()) { | ||
if (!first) System.out.print(", "); | ||
first = false; | ||
System.out.print(iter.next()); | ||
} | ||
System.out.println(); | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
src/main/java/me/alex4386/gachon/sw14462/day25/ex14_1a/Main.java
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,58 @@ | ||
package me.alex4386.gachon.sw14462.day25.ex14_1a; | ||
|
||
import java.util.ArrayList; | ||
|
||
public class Main { | ||
public static ArrayList<String> arrayToList(String[] array) { | ||
ArrayList<String> list = new ArrayList<String>(); | ||
for (String item : array) { | ||
list.add(item); | ||
} | ||
return list; | ||
} | ||
|
||
public static void removeFromArrayList(ArrayList<String> list, String s) { | ||
for (int i = 0; i < list.size(); i++) { | ||
if (list.get(i).equals(s)) { | ||
list.remove(i); | ||
i--; | ||
} | ||
} | ||
} | ||
|
||
public static <T extends Object> void printArray(T[] array) { | ||
boolean first = true; | ||
for (Object item : array) { | ||
if (!first) System.out.print(", "); | ||
first = false; | ||
System.out.print(item); | ||
} | ||
System.out.println(); | ||
} | ||
|
||
public static <U, T extends Iterable<U>> void printIterable(T iter) { | ||
boolean first = true; | ||
for (Object item : iter) { | ||
if (!first) System.out.print(", "); | ||
first = false; | ||
System.out.print(item); | ||
} | ||
System.out.println(); | ||
} | ||
|
||
public static void main(String[] args) { | ||
String[] array = {"a", "b", "c", "d", "e"}; | ||
System.out.println("Array: "); | ||
printArray(array); | ||
|
||
|
||
System.out.println("ArrayList: "); | ||
ArrayList<String> list = arrayToList(array); | ||
printIterable(list); | ||
|
||
System.out.println("Removed \"c\" from ArrayList: "); | ||
removeFromArrayList(list, "c"); | ||
printIterable(list); | ||
} | ||
|
||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/me/alex4386/gachon/sw14462/day25/ex14_1b/Main.java
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,7 @@ | ||
package me.alex4386.gachon.sw14462.day25.ex14_1b; | ||
|
||
public class Main { | ||
public static void main(String[] args) { | ||
ScoreReaderTest.main(args); | ||
} | ||
} |
Oops, something went wrong.