ok so i got my class working but im having trouble in main calling my methods and getting a lot of errors when trying to compile.
i might be confused on voids and statics in method signatures.
the first block of code and the third block of code compile fine its just the 2nd block in getting errors in
i use jgrasp because thats all we get to use on the A.P.E. so if i use eclipes i wont learn the right ways ill get sed to the shortcuts so until i pass the A.P.E. its jgrasp for me, i only say this because people tell me to use eclipse all the time :
so heres my code
appreciate any help i receive and thank you in advance for even reading this without helping.
thx PSVM
this is my class i built for rationals
import java.util.*; public class Rational1 { private int num; private int den; public Rational1() // dvc { num = 1; den = 1; } public Rational1(int num, int den) // evc { this.num = num; this.den = den; } public void setNum(int newNum) { num = newNum; } public int getNum() { return this.num; } public void setDen(int newDen) { if(newDen != 0) { den = newDen; } else { System.out.println("0 Cannot Be In The Denominator"); return; } } public int getDen() { return this.den; } public String toString() { String s = (this.num + "/" + this.den); return s; } @Override public boolean equals(Object obj) { if(obj.getClass().getSimpleName().equals("this.getClass().getSimpleName()")); { Rational1 that =(Rational1)obj; if(this.num == that.num && this.den == that.den) { return true; } } return false; } public int compareTo(Rational1 that) { int tempNum1; int tempNum2; if(this.num == that.num && this.den == that.den) { return 0; } tempNum1 = this.num * that.den; tempNum2 = this.den * that.num; return tempNum1 - tempNum2; } public static Rational1 add(Rational1 r1, Rational1 r2) { int newDen = r1.getDen() * r2.getDen(); int newNum = r1.getNum() * r2.getDen() + r2.getNum() * r1.getDen(); Rational1 newRat = new Rational1(newNum, newDen); return newRat; } public static Rational1 sub(Rational1 r1, Rational1 r2) { int newDen = r1.getDen() * r2.getDen(); int newNum = r1.getNum() * r2.getDen() - r2.getNum() * r1.getDen(); Rational1 newRat = new Rational1(newNum, newDen); return newRat; } private static int euclidGCD(int numb1, int numb2) { int remainder = 0; numb1 = Math.abs(numb1); numb2 = Math.abs(numb2); while (numb2 > 0) { remainder = numb1 % numb2; numb1 = numb2; numb2 = remainder; } return numb1; } public void Rational(int num, int den) { int GCD = euclidGCD(num,den); num = num/GCD; den = den/GCD; this.num = num; this.den = den; } }
this is the rational driver to utilize my class i wrote
import java.util.*; public class RationalDriver2 { public static void main(String ... args) { Rational1[] myRats = new Rational1[6]; myRats[0] = new Rational1(2,3); myRats[1] = new Rational1(2,18); myRats[2] = new Rational1(3,12); myRats[3] = new Rational1(9,3); myRats[4] = new Rational1(2,5); myRats[5] = new Rational1(22,7); for(Rational1 r : myRats) { System.out.println(r); } SortSearchUtil.selectionSort(myRats); for(Rational1 r : myRats) { System.out.println(r); } int option = 0; Scanner kb = new Scanner(System.in); option = displayMenu(kb); while (option != 7) { switch (option) { case 1: displayRat(); //display value break; case 2: changeOne(myRats); //change value break; case 3: addRats(); //add 2 rational and display sum break; case 4: subRats();//sub 2 rational and display difference break; case 5: sortRats(myRats); //sort array with SortSearchUtil break; case 6: printRats(myRats);//display the array to screen break; default: System.out.println("Exiting the Program\nGoodbye!"); // exit program system.exit(0); } option = displayMenu(kb); } public int displayMenu() { Scanner kb = new Scanner(System.in); System.out.println("Choose from following options:"); System.out.println("1) Display the value of Rational1 object by index."); System.out.println("2) Change the value of a rational by index."); System.out.println("3) Add two Rational1 objects together and display the sum as new Rational1 object."); System.out.println("4) Subtract two Rational1 objects and display the diference as a new Rational1 object."); System.out.println("5) Sort the array of the Rational1 number objects."); System.out.println("6) Print the array of Rational1 number objects to the screen."); System.out.println("7) Quit program."); return kb.nextInt(); } public static void displayRat() { System.out.println("For Values at Index 0 Thru 5\nWhich Index Value Do You Want Displayed?"); Scanner kb1 = new Scanner(System.in); int i = kb1.nextInt(); int x; for(x=0; x < 1; x) if(i < 0 && i > 5) { System.out.println(myRats[i]); x++; } else { System.out.println("The Value at That Index Does Not Exist\nPlease Try Again..."); return displayRat(); } } private static void changeOne(Rational1[] ara) //change rats { System.out.println("At Which Index Do You Want To Change The Rational1?"); int index = kb.nextInt(); System.out.println("What Is The Numerator of the New Rational1?"); int newNum = kb.nextInt(); System.out.println("What Is The Denominator of the New Rational1?"); int newDen = kb.nextInt(); Rational1 temp = new Rational1(newNum, newDen); ara[index] = temp; } public static Rational1 addRats()// adds two rats { System.out.println("At Which Index is the Fisrt Raional You Want To Add Located?"); int rat1 = kb.nextInt(); System.out.println("At Which Index is the Second Raional You Want To Add Located?"); int rat2 = kb.nextInt(); return Rational1.addRat(rat1, rat2); } public static Rational1 subRats()// subtract 2 rats { System.out.println("At Which Index is the Fisrt Raional You Want To Subtract Located?"); int rat1 = kb.nextInt() System.out.println("At Which Index is the Second Raional You Want To Subtract Located?"); int rat2 = kb.nextInt(); return Rational1.sub(rat1, rat2); } public static void sortRats(Rational1[] ara) { return SortSearchUtil.selectionSort(ara); } public static void printRats(Rational1[] ara) { for(i=0; i < ara.length+1; i++) { System.out.println(i); } } } }
this is my sortsearchutil which i call to sort the array or rationals
import java.util.*; import java.io.*; public class SortSearchUtil2 { public static void main(String[] args) { } //SELECTION SORT METHODS public static void selectionSort(int[] array) { int position, indexSmallest, current, temp; for (position = 0; position < array.length -1; position++) { indexSmallest = position; for(current = position + 1; current < array.length; current++) { if (array[current] < array[indexSmallest]) { indexSmallest = current; } } temp = array[position]; array[position] = array[indexSmallest]; array[indexSmallest]=temp; } } public static void selectionSort(double[] array) { int position, indexSmallest, current; double temp; for (position = 0; position < array.length -1; position++) { indexSmallest = position; for(current = position + 1; current < array.length; current++) { if (array[current] < array[indexSmallest]) { indexSmallest = current; } } temp = array[position]; array[position] = array[indexSmallest]; array[indexSmallest]=temp; } } public static void selectionSort(String[] array) { int position, indexSmallest, current; String temp; for (position = 0; position < array.length -1; position++) { indexSmallest = position; for(current = position + 1; current < array.length; current++) { if ((array[current].compareTo(array[indexSmallest]) < 0)) { indexSmallest = current; } } temp = array[position]; array[position] = array[indexSmallest]; array[indexSmallest]=temp; } } public static <Rational1> void selectionSort(Rational1[] array) { int position, indexSmallest, current; Rational1 temp; for (position = 0; position < array.length -1; position++) { indexSmallest = position; for(current = position + 1; current < array.length; current++) { if (((String) array[current]).compareTo((String) array[indexSmallest]) < 0) { indexSmallest = current; } } temp = array[position]; array[position] = array[indexSmallest]; array[indexSmallest]=temp; } } // Binary Search Method public static int binarySearch(int[] array, int target) { int high = array.length - 1; int low = 0; int mid = 0; while (low <= high) { mid = (low + high) / 2; // find the mid point.. //determine which half contains the entry... if (array[mid] > target) { high = mid - 1; // it's in the first half... } else if (array[mid] < target) { low = mid + 1; // it's in the last half... } else { return mid; } } return -1; } // Linear Search public static int linearSearch(int[] ara, int targ) { for(int i = 0; i < ara.length; i++) { if(ara[i] == targ) { return i; } } return -1; } // Insertion Sort public static void insertionSort(int[] array) { for (int index = 1; index < array.length; index++) { int key = array[index]; int position = index; while(position > 0 && key < array[position -1]) { array[position] = array[position -1]; position--; } } } // File Utility public static Scanner openInputFile(String fileName) { Scanner fileScanner = null; File fileHandle; try { fileHandle = new File(fileName); fileScanner = new Scanner(fileHandle); } catch (FileNotFoundException e) { System.out.println("The file named" + fileName + "you were looking for was not found.\nPlease check the path directory and try again.\nThank You!"); } return fileScanner; } }