Results 1 to 8 of 8

Thread: Using methods?

  1. #1
    erickGotJava is offline Member
    Join Date
    Apr 2010
    Posts
    4
    Rep Power
    0

    Default Using methods?

    I'm trying to create a program that will input a integer and output a word. I create the code and it works the way the my teacher wanted it but I can't remember how to use Methods.

    I'm asking if someone could help me use methods to split up the code, because I'm at a complete loss =\

    Heres the code, I originally wrote it in Objective-C, using Xcode because I'm more familiar with that.

    Java Code:
    int number = int(random(0,10000));
    int remainder = 0;
    int PrevNum = 0;
    int digit = 0;
    
    do{
      do{
        
      if(number <= 9 && number >= 0){
        PrevNum = number;
        remainder = number;
        number = 0;
      }
      
      if(number <= 99 && number >= 10){
      PrevNum = number;
      digit = number / 10;
      remainder = digit;
      number %= 10;
      }
      else if(number <= 999 && number >= 100){
      PrevNum = number;
      digit = number / 100;
      remainder = digit;
      number %= 100;
      }
      else if(number <= 9999 && number >= 1000){
      PrevNum = number;
      digit = number / 1000;
      remainder = digit;
      number %= 1000;
      }
       else if(number <= 99999 && number >= 10000){
      PrevNum = number;
      digit = number / 10000;
      remainder = digit;
      number %= 10000;
      }
         else if(number <= 999999 && number >= 100000){
      PrevNum = number;
      digit = number / 100000;
      remainder = digit;
      number %= 100000;
      }
    
      switch(remainder){
        
        case 1:
        if(PrevNum == 1){
          print("One ");
        }
       else if(PrevNum <= 19 && PrevNum >= 10){
        switch (PrevNum){
        
        case 10:
        print("Ten ");
        number = 0;
        break;
        
        case 11:
        print("Eleven ");
        number = 0;
        break;
        
        case 12:
        print("Twelve ");
        number = 0;
        break;
        
        case 13:
        print("Thirteen ");
        number = 0;
        break;
        
        case 14:
        print("Fourteen ");
        number = 0;
        break;
        
        case 15:
        print("Fifteen ");
        number = 0;
        break;
        
        case 16:
        print("Sixteen ");
        number = 0;
        break;
        
        case 17:
        print("Seventeen ");
        number = 0;
        break;
        
        case 18:
        print("Eighteen ");
        number = 0;
        break;
        
        case 19:
        print("Nineteen ");
        number = 0;
        break;
        
        default:
        break;
        } //End of Switch
       } //End of ElseIf
       
       else if(PrevNum <= 199 && PrevNum >= 100){
         print("One Hundred ");
       }
       else if(PrevNum <= 1999 && PrevNum >= 1000){
         print("One Thousand ");
       }
       break;
      
       
       case 2:
       if(PrevNum <= 10 && PrevNum >= 1){
            print("Two ");  
       }
       else if(PrevNum <= 29 && PrevNum >= 20){
         print("Twenty ");
       }
       else if(PrevNum <= 299 && PrevNum >= 200){
        print("Two Hundred "); 
       }
      else if(PrevNum <= 2999 && PrevNum >= 2000){
       print("Two Thousand ");
      }
      break;
      
      
       case 3:
       if(PrevNum <= 10 && PrevNum >= 1){
            print("Three ");  
       }
       else if(PrevNum <= 39 && PrevNum >= 30){
         print("Thirty ");
       }
       else if(PrevNum <= 399 && PrevNum >= 300){
        print("Three Hundred "); 
       }
      else if(PrevNum <= 3999 && PrevNum >= 3000){
       print("Three Thousand ");
      }
      break;
      
      
         case 4:
       if(PrevNum <= 10 && PrevNum >= 1){
            print("Four ");  
       }
       else if(PrevNum <= 49 && PrevNum >= 40){
         print("Forty ");
       }
       else if(PrevNum <= 499 && PrevNum >= 400){
        print("Four Hundred "); 
       }
      else if(PrevNum <= 4999 && PrevNum >= 4000){
       print("Four Thousand ");
      }
      break;
      
      
         case 5:
       if(PrevNum <= 10 && PrevNum >= 1){
            print("Five ");  
       }
       else if(PrevNum <= 59 && PrevNum >= 50){
         print("Fifty ");
       }
       else if(PrevNum <= 599 && PrevNum >= 500){
        print("Five Hundred "); 
       }
      else if(PrevNum <= 5999 && PrevNum >= 5000){
       print("Five Thousand ");
      }
      break;
      
      
           case 6:
       if(PrevNum <= 10 && PrevNum >= 1){
            print("Six ");  
       }
       else if(PrevNum <= 69 && PrevNum >= 60){
         print("Sixty ");
       }
       else if(PrevNum <= 699 && PrevNum >= 600){
        print("Six Hundred "); 
       }
      else if(PrevNum <= 6999 && PrevNum >= 6000){
       print("Six Thousand ");
      }
      break;
      
      
             case 7:
       if(PrevNum <= 10 && PrevNum >= 1){
            print("Seven ");  
       }
       else if(PrevNum <= 79 && PrevNum >= 70){
         print("Seventy ");
       }
       else if(PrevNum <= 799 && PrevNum >= 700){
        print("Seven Hundred "); 
       }
      else if(PrevNum <= 7999 && PrevNum >= 7000){
       print("Seven Thousand ");
      }
      break;
      
      
             case 8:
       if(PrevNum <= 10 && PrevNum >= 1){
            print("Eight ");  
       }
       else if(PrevNum <= 89 && PrevNum >= 80){
         print("Eighty ");
       }
       else if(PrevNum <= 899 && PrevNum >= 800){
        print("Eight Hundred "); 
       }
      else if(PrevNum <= 8999 && PrevNum >= 8000){
       print("Eight Thousand ");
      }
      break;
      
      
             case 9:
       if(PrevNum <= 10 && PrevNum >= 1){
            print("Nine ");  
       }
       else if(PrevNum <= 99 && PrevNum >= 90){
         print("Ninety ");
       }
       else if(PrevNum <= 999 && PrevNum >= 900){
        print("Nine Hundred "); 
       }
      else if(PrevNum <= 9999 && PrevNum >= 9000){
       print("Nine Thousand ");
      }
      break;
      
      
      case 0:
      if(number == 0 && PrevNum == 0){
        if(digit == 0){
          print("Zero");
        }
      }
      else{
        print("");
      }
      digit = 0;
      break;
      
      default: 
      print("");
      break;
      }
    }while(number != 0);
    }while(digit != 0);

  2. #2
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    That's a lot of code :)

    let's consider this number:

    nine thousand three hundred forty five

    it's broken down like this:


    [nine thousand] [three hundred] [forty] [five]

    so your code should look like this:

    Java Code:
    int number = random number; // you're calculating it already
    printThousands (number);
    printHundreds (number);
    printTens(number);
    printOnes (number);
    printHundreds(), as one example, would look like this:
    Java Code:
    private void printHundreds (int number) {
      number = (number % 1000) / 100;
      if (number == 0) { // print nothing }
      else {
        printDigitName (number);
        System.out.print (" hundred");
      }
    }

  3. #3
    erickGotJava is offline Member
    Join Date
    Apr 2010
    Posts
    4
    Rep Power
    0

    Default

    Sorry, the main reason I don't understand this is because I have no idea how but more importantly WHY functions work.

    I would write something like this

    Java Code:
      
      
    class Blah{
    int number = int(random(0,100));
    
      
    int output(int remainder, int PrevNum, int number, int digit){
    
     if(number <= 9 && number >= 0){
        PrevNum = number;
        remainder = number;
        number = 0;
      }
      
      if(number <= 99 && number >= 10){
      PrevNum = number;
      digit = number / 10;
      remainder = digit;
      number %= 10;
      }
      else if(number <= 999 && number >= 100){
      PrevNum = number;
      digit = number / 100;
      remainder = digit;
      number %= 100;
      }
      else if(number <= 9999 && number >= 1000){
      PrevNum = number;
      digit = number / 1000;
      remainder = digit;
      number %= 1000;
      }
       else if(number <= 99999 && number >= 10000){
      PrevNum = number;
      digit = number / 10000;
      remainder = digit;
      number %= 10000;
      }
         else if(number <= 999999 && number >= 100000){
      PrevNum = number;
      digit = number / 100000;
      remainder = digit;
      number %= 100000;
      }
    return remainder;
    
    }  
    
    String word(int remainder, int PrevNum, int number, int digit,int output){
      
      String result = "";
      
      switch(remainder){
        
        case 1:
        if(PrevNum == 1){
          result ="One ";
        }
       else if(PrevNum <= 19 && PrevNum >= 10){
        switch (PrevNum){
        
        case 10:
        result = "Ten ";
        number = 0;
        break;
        
        case 11:
        result ="Eleven ";
        number = 0;
        break;
        
        case 12:
        result ="Twelve ";
        number = 0;
        break;
        
        case 13:
        result ="Thirteen ";
        number = 0;
        break;
        
        case 14:
        result ="Fourteen ";
        number = 0;
        break;
        
        case 15:
        result ="Fifteen ";
        number = 0;
        break;
        
        case 16:
        result ="Sixteen ";
        number = 0;
        break;
        
        case 17:
        result ="Seventeen ";
        number = 0;
        break;
        
        case 18:
        result ="Eighteen ";
        number = 0;
        break;
        
        case 19:
        result ="Nineteen ";
        number = 0;
        break;
        
        default:
        break;
        } //End of Switch
       } //End of ElseIf
       
       else if(PrevNum <= 199 && PrevNum >= 100){
         result ="One Hundred ";
       }
       else if(PrevNum <= 1999 && PrevNum >= 1000){
         result ="One Thousand ";
       }
       break;
      
       
       case 2:
       if(PrevNum <= 10 && PrevNum >= 1){
            result ="Two ";  
       }
       else if(PrevNum <= 29 && PrevNum >= 20){
         result ="Twenty ";
       }
       else if(PrevNum <= 299 && PrevNum >= 200){
        result ="Two Hundred "; 
       }
      else if(PrevNum <= 2999 && PrevNum >= 2000){
       result ="Two Thousand ";
      }
      break;
      
      
       case 3:
       if(PrevNum <= 10 && PrevNum >= 1){
            result ="Three ";  
       }
       else if (PrevNum <= 39 && PrevNum >= 30){
         result = "Thirty ";
       }
       else if(PrevNum <= 399 && PrevNum >= 300){
        result = "Three Hundred "; 
       }
      else if(PrevNum <= 3999 && PrevNum >= 3000){
       result = "Three Thousand ";
      }
      break;
      
      
         case 4:
       if(PrevNum <= 10 && PrevNum >= 1){
            result = "Four ";  
       }
       else if(PrevNum <= 49 && PrevNum >= 40){
         result = "Forty ";
       }
       else if(PrevNum <= 499 && PrevNum >= 400){
        result = "Four Hundred "; 
       }
      else if(PrevNum <= 4999 && PrevNum >= 4000){
       result = "Four Thousand ";
      }
      break;
      
      
         case 5:
       if(PrevNum <= 10 && PrevNum >= 1){
            result = "Five ";  
       }
       else if(PrevNum <= 59 && PrevNum >= 50){
         result = "Fifty ";
       }
       else if(PrevNum <= 599 && PrevNum >= 500){
        result = "Five Hundred "; 
       }
      else if(PrevNum <= 5999 && PrevNum >= 5000){
       result = "Five Thousand ";
      }
      break;
      
      
           case 6:
       if(PrevNum <= 10 && PrevNum >= 1){
            result = "Six ";  
       }
       else if(PrevNum <= 69 && PrevNum >= 60){
         result = "Sixty ";
       }
       else if(PrevNum <= 699 && PrevNum >= 600){
        result = "Six Hundred "; 
       }
      else if(PrevNum <= 6999 && PrevNum >= 6000){
       result = "Six Thousand ";
      }
      break;
      
      
             case 7:
       if(PrevNum <= 10 && PrevNum >= 1){
            result = "Seven ";  
       }
       else if(PrevNum <= 79 && PrevNum >= 70){
         result = "Seventy ";
       }
       else if(PrevNum <= 799 && PrevNum >= 700){
        result = "Seven Hundred "; 
       }
      else if(PrevNum <= 7999 && PrevNum >= 7000){
       result = "Seven Thousand ";
      }
      break;
      
      
             case 8:
       if(PrevNum <= 10 && PrevNum >= 1){
            result ="Eight ";  
       }
       else if(PrevNum <= 89 && PrevNum >= 80){
         result ="Eighty ";
       }
       else if(PrevNum <= 899 && PrevNum >= 800){
        result ="Eight Hundred "; 
       }
      else if(PrevNum <= 8999 && PrevNum >= 8000){
       result ="Eight Thousand ";
      }
      break;
      
      
             case 9:
       if(PrevNum <= 10 && PrevNum >= 1){
            result ="Nine ";  
       }
       else if(PrevNum <= 99 && PrevNum >= 90){
         result ="Ninety ";
       }
       else if(PrevNum <= 999 && PrevNum >= 900){
        result ="Nine Hundred "; 
       }
      else if(PrevNum <= 9999 && PrevNum >= 9000){
       result ="Nine Thousand ";
      }
      break;
      
      
      case 0:
      if(number == 0 && PrevNum == 0){
        if(digit == 0){
          result ="Zero";
        }
      }
      else{
        result = "";
      }
      digit = 0;
      break;
      
      default: 
      result ="";
      break;
      }
      return result;
    
    }
    
    void setup(){
    
    }
    }
    I know its wrong but have no idea WHY its wrong. =\ the whole concept is a lost to me right now and its due at 11:59pm arghh

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    It's lots of coding. Very difficult to go through it. If you can explain your question specifically, it's easy for us to comment/help on your question.

  5. #5
    erickGotJava is offline Member
    Join Date
    Apr 2010
    Posts
    4
    Rep Power
    0

    Default

    Here is the worksheet I'm using. I'm about to just submit the code the way I originally had it =\ partial credit is better then a 0


    ntroduction
    Homework #3 Functions

    In class we have been discussing how to write our own functions for a program. We have considered the function digit() , revDigits(), and digits(). In this assignment you will take these functions and modify them, as needed, to produce a more correct English representation of the numbers being processed. Make sure that your program includes comments as used in the samples and also include testing code, also as used in the samples. Remember, consistency and style DO count here.
    Rules for Number Representation

    1. If the low order two digits of the number are under twenty but greater than nine, then you will display the strings: ten, eleven, twelve, thirteen, fourteen, fifteen, sixteen, seventeen, eighteen, and nineteen.

    2. The next to last digits will display the strings: twenty, thirty, forty, fifty, sixty, seventy, eighty, and ninety.

    3. The third to last digit will display the number of hundreds (third to last digit) followed by the word hundred.

    4. In a similar fashion, the fourth, fifth, and sixth to last digits will be written as above but followed by the word thousand.

    5. Likewise for million, and billion. int values are limited to about 9 digits so they cannot grow beyond billions.

    6. As appropriate, when a digit is zero and needs to be followed by hundred, thousand, million, or billion then the value may be skipped (see examples below).
    I've been reading around and some people say Java doesn't have Functions, only Methods and my professor said their the same thing o.o...I'm so confused. I'm not trying to cheat or anything, I created the code but just need some help on it to make they way he wants it.

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Both functions and method are the same, word use interchangeably in different languages.

    On your assignment, did you do the part one? Display the string from nine to twenty. It's not that much difficult. Can you show it here?

  7. #7
    erickGotJava is offline Member
    Join Date
    Apr 2010
    Posts
    4
    Rep Power
    0

    Default

    Java Code:
    String digit(int number)
    {
       String result = "";
    
    switch (abs(number) % 10){
    case 0:
    result = "zero";
    break;
    
    case 1: 
    result = "one";
    break;
    
    case 2: 
    result = "two";
    break;
    
    case 3: 
    result = "three";
    break;
    
    case 4: 
    result = "four";
    break;
    
    case 5: 
    result = "five";
    break;
    
    
    case 6: 
    result = "six";
    break;
    
    case 7:
    result = "seven";
    break;
    
    case 8: 
    result = "eight";
    break;
    
    case 9:
    result = "nine";
    break;
    }
    return result;
    }
    
    void setup(){
    Test t = new Test();
    
    t.check(digits(0), "zero");
    }
    Thats all he gave me... I have no idea what it means. What I put it into Processing, it gives me a error.

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    In first part you want to do is something like this.

    Say you decide (or the user) a number. If the number is grater than 9 and less than 20 your application just print the text as ten, eleven, twelve, ......

    What the above code doing is that number which is decide by your or the user divide by 10 and display the text of the remainder. For example is the number if 13, divide by 10, the reminder is 3, so the text is three. Your assignment ask you to modify those things according to.

    In very simple way,

    Java Code:
       private void displayMessage(int val) {
            if (val > 9 && val < 20) {
                partOne(val);
            }
        }
    
        private void partOne(int val) {
            switch (val) {
                case 10:
                    System.out.println("ten");
                    break;
                case 11:
                    System.out.println("eleven");
                    break;
                case 12:
                    System.out.println("twelve");
                    break;
                case 13:
                    System.out.println("thirteen");
                    break;
                case 14:
                    System.out.println("fourteen");
                    break;
                case 15:
                    System.out.println("fifteen");
                    break;
                case 16:
                    System.out.println("sixteen");
                    break;
                case 17:
                    System.out.println("seventeen");
                    break;
                case 18:
                    System.out.println("eighteen");
                    break;
                case 19:
                    System.out.println("nineteen");
                    break;
            }
        }

Similar Threads

  1. Replies: 8
    Last Post: 04-16-2012, 11:00 PM
  2. Methods
    By soccer_kid_6 in forum New To Java
    Replies: 15
    Last Post: 02-27-2010, 11:49 AM
  3. Help with get and set methods
    By shroomiin in forum New To Java
    Replies: 4
    Last Post: 11-23-2009, 08:04 PM
  4. Replies: 4
    Last Post: 04-03-2009, 06:36 AM
  5. Get and Set Methods
    By kian_hong2000 in forum New To Java
    Replies: 10
    Last Post: 08-27-2008, 02:06 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •