Skip to content

Commit

Permalink
chore: implemented ex14
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex4386 committed May 29, 2024
1 parent 4ff1112 commit edd94c8
Show file tree
Hide file tree
Showing 16 changed files with 643 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/main/java/me/alex4386/gachon/sw14462/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import java.util.*;

public class Main {
public static String currentTarget = "day24";
public static String currentTarget = "day25";
public static boolean fallbackToLatest = true;

public static Map<String, Class<?>> getAvailableTargetClassNames() {
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/me/alex4386/gachon/sw14462/day25/Main.java
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 src/main/java/me/alex4386/gachon/sw14462/day25/ex14_1/Main.java
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 src/main/java/me/alex4386/gachon/sw14462/day25/ex14_1/Pet.java
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");
}

}
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 src/main/java/me/alex4386/gachon/sw14462/day25/ex14_11/Main.java
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 src/main/java/me/alex4386/gachon/sw14462/day25/ex14_1a/Main.java
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);
}

}
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);
}
}
Loading

0 comments on commit edd94c8

Please sign in to comment.