Browse Source

Old changes I don't recongnize.

Tom Flucke 10 năm trước cách đây
mục cha
commit
37ad89b813
3 tập tin đã thay đổi với 134 bổ sung0 xóa
  1. 45 0
      Lab8/MyList.java
  2. 78 0
      Lab8/MyListDriver.java
  3. 11 0
      Project2/students.txt

+ 45 - 0
Lab8/MyList.java

@@ -0,0 +1,45 @@
+
+public class MyList {
+	
+	private class Node{
+		public int element;
+		public Node next;
+	}
+	//instance variables
+	public Node head;
+	//constructor
+	public MyList(){
+		head = null;
+	}
+	//methods
+	public void add(int elm){
+		Node newNode = new Node();
+		newNode.element = elm;
+		newNode.next = head;
+		head = newNode;
+	}
+	public boolean find(int elm){
+		return find(elm, head);
+	}
+	private boolean find(int elm, Node next){
+		if (next == null) return false;
+		if (next.element == elm) return true;
+		return find(elm, next.next);
+	}
+	public void print(){
+		print(head);
+	}
+	private void print(Node next){
+		if (next != null){
+			print(next.next);
+			System.out.print(next.element + " ");
+		}
+	}
+	public int sum(){
+		return sum(head);
+	}
+	private int sum(Node next){
+		if (next == null) return 0;
+		return sum(next.next) + next.element;
+	}
+}

+ 78 - 0
Lab8/MyListDriver.java

@@ -0,0 +1,78 @@
+import java.util.Scanner;
+
+public class MyListDriver {
+	public static void main(String[] args) {
+		Scanner scan = new Scanner(System.in);
+		MyList intList = new MyList();
+		
+		System.out.println("Choose one of the following operations:");
+        System.out.println("- add (enter the letter a)");
+        System.out.println("- find (enter the letter f)");
+        System.out.println("- print (enter the letter p)");
+        System.out.println("- sum (enter the letter s)");
+        System.out.println("- quit (enter the letter q)");
+        
+        String temp = scan.nextLine();
+        
+        while (temp.length() != 1) {
+            System.out.println("Invalid choice");
+            System.out.println("Do another operation? (refer to menu above)");
+            temp = scan.nextLine();
+        }
+        
+        char answer = temp.charAt(0);
+        
+        while (answer != 'q'){
+            switch (answer){
+                case 'a':
+                    System.out.println("What int do you want to add?");
+                    if(scan.hasNextInt()){
+                        int item = scan.nextInt();
+                        intList.add(item);
+                        System.out.println(item+" added");
+                    }
+                    else System.out.println("Invalid value");
+                    scan.nextLine();
+                    break;
+                case 'f':
+                	System.out.println("What int do you want to search for?");
+                    if(scan.hasNextInt()){
+                        int item = scan.nextInt();
+                        if (intList.find(item)) System.out.println(item+" found");
+                        else System.out.println(item+" not found");
+                    }
+                    else System.out.println("Invalid value");
+                    scan.nextLine();
+                    break;
+                    
+                case 'p':
+                    intList.print();
+                    System.out.println();
+                    break;
+                    
+                case 's':
+                    System.out.println("Sum of all values is " + intList.sum());
+                    break;
+                    
+                default:
+                    System.out.println("Invalid case");
+                    break;
+            }
+            
+            System.out.println("Do another operation? (refer to menu above)");
+            temp = scan.nextLine();
+            
+            while (temp.length() != 1) {
+                System.out.println("Invalid choice");
+                System.out.println("Do another operation? (refer to menu above)");
+                temp = scan.nextLine();
+            }
+            
+            answer = temp.charAt(0);
+        }
+        scan.close();
+        System.out.println("quitting");
+        System.out.println();
+	}
+
+}

+ 11 - 0
Project2/students.txt

@@ -0,0 +1,11 @@
+
+2 Monkey
+7 Horse
+345
+-45 Chicken
+Duck 6
+5 Lara
+4 Ape
+9 Panda Hi    
+3 2
+2147483683 Cat