Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: oop rational number help

  1. #1
    Junior Member PublicStaticVoidMain's Avatar
    Join Date
    Mar 2014
    Location
    Spokane, Washington
    Posts
    6
    My Mood
    Stressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default oop rational number help

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


  2. #2
    Member
    Join Date
    Dec 2013
    Posts
    40
    My Mood
    Amazed
    Thanks
    2
    Thanked 11 Times in 11 Posts

    Default Re: oop rational number help

    Could you post the complete errors you get when you compile?

  3. #3
    Junior Member PublicStaticVoidMain's Avatar
    Join Date
    Mar 2014
    Location
    Spokane, Washington
    Posts
    6
    My Mood
    Stressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: oop rational number help

    Quote Originally Posted by Mugambbo View Post
    Could you post the complete errors you get when you compile?
    sure thing here they are

    RationalDriver2.java:57: error: illegal start of expression
          public int displayMenu()
          ^
    RationalDriver2.java:57: error: ';' expected
          public int displayMenu()
                                ^
    RationalDriver2.java:70: error: illegal start of expression
          public static void displayRat()
          ^
    RationalDriver2.java:70: error: illegal start of expression
          public static void displayRat()
                 ^
    RationalDriver2.java:70: error: ';' expected
          public static void displayRat()
                       ^
    RationalDriver2.java:70: error: ';' expected
          public static void displayRat()
                                         ^
    RationalDriver2.java:76: error: not a statement
             for(x=0; x < 1; x)
                             ^
    RationalDriver2.java:88: error: illegal start of expression
          private static void changeOne(Rational1[] ara) //change rats
          ^
    RationalDriver2.java:88: error: illegal start of expression
          private static void changeOne(Rational1[] ara) //change rats
                  ^
    RationalDriver2.java:88: error: ';' expected
          private static void changeOne(Rational1[] ara) //change rats
                        ^
    RationalDriver2.java:88: error: '.class' expected
          private static void changeOne(Rational1[] ara) //change rats
                                                    ^
    RationalDriver2.java:88: error: ';' expected
          private static void changeOne(Rational1[] ara) //change rats
                                                       ^
    RationalDriver2.java:100: error: illegal start of expression
          public static Rational1 addRats()// adds two rats
          ^
    RationalDriver2.java:100: error: illegal start of expression
          public static Rational1 addRats()// adds two rats
                 ^
    RationalDriver2.java:100: error: ';' expected
          public static Rational1 addRats()// adds two rats
                                 ^
    RationalDriver2.java:100: error: ';' expected
          public static Rational1 addRats()// adds two rats
                                           ^
    RationalDriver2.java:109: error: illegal start of expression
          public static Rational1 subRats()// subtract 2 rats
          ^
    RationalDriver2.java:109: error: illegal start of expression
          public static Rational1 subRats()// subtract 2 rats
                 ^
    RationalDriver2.java:109: error: ';' expected
          public static Rational1 subRats()// subtract 2 rats
                                 ^
    RationalDriver2.java:109: error: ';' expected
          public static Rational1 subRats()// subtract 2 rats
                                           ^
    RationalDriver2.java:112: error: ';' expected
             int rat1 = kb.nextInt()
                                    ^
    RationalDriver2.java:118: error: illegal start of expression
          public static void sortRats(Rational1[] ara)// sorts rats
          ^
    RationalDriver2.java:118: error: illegal start of expression
          public static void sortRats(Rational1[] ara)// sorts rats
                 ^
    RationalDriver2.java:118: error: ';' expected
          public static void sortRats(Rational1[] ara)// sorts rats
                       ^
    RationalDriver2.java:118: error: '.class' expected
          public static void sortRats(Rational1[] ara)// sorts rats
                                                  ^
    RationalDriver2.java:118: error: ';' expected
          public static void sortRats(Rational1[] ara)// sorts rats
                                                     ^
    RationalDriver2.java:122: error: illegal start of expression
          public static void printRats(Rational1[] ara) // prints rats
          ^
    RationalDriver2.java:122: error: illegal start of expression
          public static void printRats(Rational1[] ara) // prints rats
                 ^
    RationalDriver2.java:122: error: ';' expected
          public static void printRats(Rational1[] ara) // prints rats
                       ^
    RationalDriver2.java:122: error: '.class' expected
          public static void printRats(Rational1[] ara) // prints rats
                                                   ^
    RationalDriver2.java:122: error: ';' expected
          public static void printRats(Rational1[] ara) // prints rats
                                                      ^
    31 errors

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: oop rational number help

    The kind of errors you're seeing usually indicate that there's a curly brace out of place which causes a method to begin inside another method - a no no - or a method or class that never ends, etc. In this case, the code defines methods inside RationalDriver2's main() method (the no no I mentioned earlier). Decide where your main() method ends and add an ending curly brace. That will probably require you to remove an existing ending curly brace later on.

    Eclipse will help you sort this out by properly indenting (or formatting) your code for you. After doing that, you should see that there are methods following the main() method that are more indented than the main() method. That greater level of indentation indicates that the methods exist inside the main() method.

  5. #5
    Junior Member PublicStaticVoidMain's Avatar
    Join Date
    Mar 2014
    Location
    Spokane, Washington
    Posts
    6
    My Mood
    Stressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: oop rational number help

    Quote Originally Posted by GregBrannon View Post
    The kind of errors you're seeing usually indicate that there's a curly brace out of place which causes a method to begin inside another method - a no no - or a method or class that never ends, etc. In this case, the code defines methods inside RationalDriver2's main() method (the no no I mentioned earlier). Decide where your main() method ends and add an ending curly brace. That will probably require you to remove an existing ending curly brace later on.

    Eclipse will help you sort this out by properly indenting (or formatting) your code for you. After doing that, you should see that there are methods following the main() method that are more indented than the main() method. That greater level of indentation indicates that the methods exist inside the main() method.
    thank you after some research i came to the same conclusion had my mians closing "}" after all my methods moved it up to the right spot and went form 52 to 11 errors, but now im down to 1 error and this is it
    SortSearchUtil.java:50: error: missing return statement
       }
       ^
    1 error

    this is my call and method
    if someoen could tell me whats wrong that would be very much appreciated, ive changed form bject to void to ara[] and can't seem to get it going.

    public static Rational1 sortRats(Rational1[] ara) // sorts rats
       {
          return SortSearchUtil.selectionSort(ara);
       }
    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;
          }
       }

    TYVM - PSVM

  6. #6
    Junior Member PublicStaticVoidMain's Avatar
    Join Date
    Mar 2014
    Location
    Spokane, Washington
    Posts
    6
    My Mood
    Stressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Cool Re: oop rational number help

    got it working.

    if anyone is having similar probs and wants to see working code pm me

    thx PSVM

Similar Threads

  1. Rational - GCD issue
    By kay19 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 5th, 2013, 03:38 AM
  2. Rational program calling up from different file but in same package
    By my21 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 11th, 2013, 12:00 PM
  3. Defining a rational class. Simple but i cant figure it out to save my life
    By spetillo3 in forum Object Oriented Programming
    Replies: 3
    Last Post: October 26th, 2011, 11:02 PM
  4. The Rational Class Program [HOMEWORK HELP]
    By FCarlton24 in forum Object Oriented Programming
    Replies: 1
    Last Post: October 18th, 2011, 12:31 PM
  5. [Homework] Rational Class
    By burger king in forum Object Oriented Programming
    Replies: 2
    Last Post: January 13th, 2011, 09:15 PM