diff --git a/labs/lab10/Backwards.java b/labs/lab10/Backwards.java new file mode 100644 index 0000000..27d03b2 --- /dev/null +++ b/labs/lab10/Backwards.java @@ -0,0 +1,35 @@ +// ****************************************************************** +// Backwards.java +// +// Uses a recursive method to print a string backwards. +// ****************************************************************** +import java.util.Scanner; +public class Backwards +{ + //-------------------------------------------------------------- + // Reads a string from the user and prints it backwards. + //-------------------------------------------------------------- + public static void main(String[] args) + { + String msg; + Scanner scan = new Scanner(System.in); + System.out.print("Enter a string: "); + msg = scan.nextLine(); + System.out.print("\nThe string backwards: "); + printBackwards(msg); + System.out.println(); + } + + //-------------------------------------------------------------- + // Takes a string and recursively prints it backwards. + //-------------------------------------------------------------- + public static void printBackwards(String s) + { + if (s.isEmpty()) { + return; + } + System.out.print(s.charAt(s.length() - 1)); + printBackwards(s.substring(0, s.length() - 1)); + // Fill in code + } +} \ No newline at end of file diff --git a/labs/lab10/CountLettere.java b/labs/lab10/CountLettere.java new file mode 100644 index 0000000..9f0b0b6 --- /dev/null +++ b/labs/lab10/CountLettere.java @@ -0,0 +1,36 @@ +// **************************************************************** +// CountLetters.java +// +// Reads a words from the standard input and prints the number of +// occurrences of each letter in that word. +// +// **************************************************************** +import java.util.Scanner; +public class CountLetterse +{ + public static void main(String[] args) + { + int[] counts = new int[26]; + Scanner scan = new Scanner(System.in); + //get word from user + System.out.print("Enter a single word (letters only, please): "); + String word = scan.nextLine(); + //convert to all upper case + word = word.toUpperCase(); + //count frequency of each letter in string + for (int i=0; i < word.length(); i++) { + try { + char current = word.charAt(i); + counts[word.charAt(i)-'A']++; + } + catch (Exception e){ + System.out.println("Reason for error:"+"!"+(char)word.charAt(i)+"!"); + } + } + //print frequencies + System.out.println(); + for (int i=0; i < counts.length; i++) + if (counts [i] != 0) + System.out.println((char)(i +'A') + ": " + counts[i]); + } +} \ No newline at end of file diff --git a/labs/lab10/CountLetters.java b/labs/lab10/CountLetters.java new file mode 100644 index 0000000..1ace707 --- /dev/null +++ b/labs/lab10/CountLetters.java @@ -0,0 +1,33 @@ +// **************************************************************** +// CountLetters.java +// +// Reads a words from the standard input and prints the number of +// occurrences of each letter in that word. +// +// **************************************************************** +import java.util.Scanner; +public class CountLetters +{ + public static void main(String[] args) + { + int[] counts = new int[26]; + Scanner scan = new Scanner(System.in); + //get word from user + System.out.print("Enter a single word (letters only, please): "); + String word = scan.nextLine(); + //convert to all upper case + word = word.toUpperCase(); + //count frequency of each letter in string + for (int i=0; i < word.length(); i++) { + char current = word.charAt(i); + if (current >= 'A' && current <= 'Z') { + counts[word.charAt(i)-'A']++; + } + } + //print frequencies + System.out.println(); + for (int i=0; i < counts.length; i++) + if (counts [i] != 0) + System.out.println((char)(i +'A') + ": " + counts[i]); + } +} \ No newline at end of file diff --git a/labs/lab10/Power.java b/labs/lab10/Power.java new file mode 100644 index 0000000..028335a --- /dev/null +++ b/labs/lab10/Power.java @@ -0,0 +1,42 @@ +// **************************************************************** +// Power.java +// +// Reads in two integers and uses a recursive power method +// to compute the first raised to the second power. +// **************************************************************** +import java.util.Scanner; +public class Power +{ + public static void main(String[] args) + { + int base, exp; + int answer; + Scanner scan = new Scanner(System.in); + System.out.print("Welcome to the power program! "); + System.out.println("Please use integers only."); + + //get base + System.out.print("Enter the base you would like raised to a power: "); + base = scan.nextInt(); + //get exponent + System.out.print("Enter the power you would like it raised to: "); + exp = scan.nextInt(); + answer = power(base,exp); + System.out.println(base + " raised to the " + exp + " is " + answer); + } + // ------------------------------------------------- + // Computes and returns base^exp + // ------------------------------------------------- + public static int power(int base, int exp) + { + int pow; + if (exp == 0) { + pow = 1; + } else { + pow = base*power(base, exp-1); + } + return pow; + //otherwise set pow to base*base^(exp-1) + //return pow + } +} diff --git a/labs/lab6/Account.class b/labs/lab6/Account.class index 0892538..92fb41d 100644 Binary files a/labs/lab6/Account.class and b/labs/lab6/Account.class differ diff --git a/labs/lab6/Account.java b/labs/lab6/Account.java index 09364d5..d8a32ac 100644 --- a/labs/lab6/Account.java +++ b/labs/lab6/Account.java @@ -9,6 +9,7 @@ public class Account private double balance; private String name; private long acctNum; + private static int numAccounts = 0; //---------------------------------------------- //Constructor -- initializes balance, owner, and account number //---------------------------------------------- @@ -17,15 +18,18 @@ public class Account balance = initBal; name = owner; acctNum = number; + ++numAccounts; } public Account(double initBal, String owner) { balance = initBal; name = owner; + ++numAccounts; } public Account(String owner) { name = owner; + ++numAccounts; } //---------------------------------------------- // Checks to see if balance is sufficient for withdrawal. @@ -59,6 +63,16 @@ public class Account { return balance; } +// Static + public static int getNumAccounts() + { + return numAccounts; + } + + public void close () { + name += " Closed"; + + } //---------------------------------------------- // Returns a string containing the name, account number, and balance. //---------------------------------------------- @@ -66,6 +80,7 @@ public class Account { return "Name:" + name + "\nAccount Number: " + acctNum + -"\nBalance: " + balance; +"\nBalance: " + balance; +//"\nTotal account number: " + numAccounts; } } diff --git a/labs/lab7/IntList.class b/labs/lab7/IntList.class new file mode 100644 index 0000000..3111d0a Binary files /dev/null and b/labs/lab7/IntList.class differ diff --git a/labs/lab7/IntList.java b/labs/lab7/IntList.java new file mode 100644 index 0000000..693a50f --- /dev/null +++ b/labs/lab7/IntList.java @@ -0,0 +1,50 @@ +// **************************************************************** +// IntList.java +// +// An (unsorted) integer list class with a method to add an +// integer to the list and a toString method that returns the contents +// of the list with indices. +// **************************************************************** +public class IntList { + protected int[] list; + protected int numElements = 0; + + //------------------------------------------------------------- + // Constructor -- creates an integer list of a given size. + //------------------------------------------------------------- + public IntList(int size) { + list = new int[size]; + } + + //------------------------------------------------------------- + // Adds an integer to the list. If the list is full, + // prints a message and does nothing. + //------------------------------------------------------------- + public void add(int value) { + if (numElements == list.length) { + System.out.println("Can't add, list is full"); + } else { + list[numElements] = value; + numElements++; + } + } + + //------------------------------------------------------------- + // Returns the list elements with indices as a string. + //------------------------------------------------------------- + @Override + public String toString() { + StringBuilder result = new StringBuilder(); + for (int i = 0; i < numElements; i++) { + result.append(i).append(": ").append(list[i]).append("\n"); + } + return result.toString(); + } +} + +// ------------------------------------------------------------- +// Interface for sorting numbers. +// ------------------------------------------------------------- +interface NumberSorter { + void sortNumbers(); +} \ No newline at end of file diff --git a/labs/lab7/ListTest.class b/labs/lab7/ListTest.class new file mode 100644 index 0000000..6932b7c Binary files /dev/null and b/labs/lab7/ListTest.class differ diff --git a/labs/lab7/ListTest.java b/labs/lab7/ListTest.java new file mode 100644 index 0000000..3ef22c1 --- /dev/null +++ b/labs/lab7/ListTest.java @@ -0,0 +1,29 @@ +// **************************************************************** +// ListTest.java +// +// A simple test program that creates an IntList, puts some +// ints in it, and prints the list. +// +// **************************************************************** +public class ListTest +{ + public static void main(String[] args) + { + IntList myList = new IntList(10); + myList.add(100); + myList.add(50); + myList.add(200); + myList.add(25); + System.out.println(myList); + + IntList intList = new IntList(10); + intList.add(10); + intList.add(5); + intList.add(20); + intList.add(15); + + // Print the unsorted list + System.out.println("IntList contents:"); + System.out.println(intList); + } +} \ No newline at end of file diff --git a/labs/lab7/NumberSorter.class b/labs/lab7/NumberSorter.class new file mode 100644 index 0000000..a08de1f Binary files /dev/null and b/labs/lab7/NumberSorter.class differ diff --git a/labs/lab7/Player.java b/labs/lab7/Player.java new file mode 100644 index 0000000..9b9d60b --- /dev/null +++ b/labs/lab7/Player.java @@ -0,0 +1,43 @@ +// ********************************************************** +// Player.java +// +// Defines a Player class that holds information about an athlete. +// ********************************************************** +import java.util.Scanner; +public class Player +{ + private String name; + private String team; + private int jerseyNumber; + //----------------------------------------------------------- + // Prompts for and reads in the player's name, team, and + // jersey number. + //----------------------------------------------------------- + public void readPlayer() + { + Scanner scan = new Scanner(System.in); + System.out.print("Name: "); + name = scan.nextLine(); + System.out.print("Team: "); + team = scan.nextLine(); + System.out.print("Jersey number: "); + jerseyNumber = Scan.nextInt(); + } + +@Override + public boolean equals(Object obj) { + if (this == obj) return true; // Check if the two references are the same + if (obj == null || getClass() != obj.getClass()) return false; // Ensure obj is a Player + + Player other = (Player) obj; // Typecast obj to Player + return team.equals(other.team) && jerseyNumber == other.jerseyNumber; + } + + //----------------------------------------------------------- + // Returns a string representation of the player. + //----------------------------------------------------------- + @Override + public String toString() { + return "Name: " + name + ", Team: " + team + ", Jersey Number: " + jerseyNumber; + } +} \ No newline at end of file diff --git a/labs/lab7/SortedIntList.class b/labs/lab7/SortedIntList.class new file mode 100644 index 0000000..241ecbe Binary files /dev/null and b/labs/lab7/SortedIntList.class differ diff --git a/labs/lab7/SortedIntList.java b/labs/lab7/SortedIntList.java new file mode 100644 index 0000000..86dbadf --- /dev/null +++ b/labs/lab7/SortedIntList.java @@ -0,0 +1,39 @@ +// **************************************************************** +// SortedIntList.java +// +// A subclass of IntList that sorts the numbers in descending order. +// **************************************************************** +class SortedIntList extends IntList implements NumberSorter { + //------------------------------------------------------------- + // Constructor -- creates an integer list of a given size. + //------------------------------------------------------------- + public SortedIntList(int size) { + super(size); + } + + //------------------------------------------------------------- + // Sorts the numbers in descending order. + //------------------------------------------------------------- + @Override + public void sortNumbers() { + for (int i = 0; i < numElements - 1; i++) { + for (int j = 0; j < numElements - i - 1; j++) { + if (list[j] < list[j + 1]) { + // Swap list[j] and list[j + 1] + int temp = list[j]; + list[j] = list[j + 1]; + list[j + 1] = temp; + } + } + } + } + + //------------------------------------------------------------- + // Adds an integer to the list and sorts it. + //------------------------------------------------------------- + @Override + public void add(int value) { + super.add(value); + sortNumbers(); + } +} \ No newline at end of file diff --git a/labs/lab7/chapter8_lab-7.pdf b/labs/lab7/chapter8_lab-7.pdf new file mode 100644 index 0000000..8e6d46d Binary files /dev/null and b/labs/lab7/chapter8_lab-7.pdf differ diff --git a/labs/lab7/chapter8_lab-7.pdfZone.Identifier b/labs/lab7/chapter8_lab-7.pdfZone.Identifier new file mode 100644 index 0000000..e69de29