Java Methods

Discussion in 'Web Design & Coding' started by PseudoKiller, Mar 29, 2004.

  1. PseudoKiller

    PseudoKiller Zug Zug

    Messages:
    3,858
    Location:
    Ice Crown Citadel
    I need to...

    Write a Java program with the following methods:

    Named ‘first’ that will input a number and return it.
    Named ‘second’ that will input a number and return it.
    Named ‘small’ that will find the smaller of the two numbers and return it.


    This is what I have so far...

    Code:
    [font=Courier New][color=#941edf]
    import[/color][/font][font=Courier New] cs1.Keyboard; //this is used for Keyboard inputs
     
    [/font][font=Courier New][color=#941edf]public [/color][/font][font=Courier New][color=#941edf]class[/color][/font][font=Courier New] BigSmall
     
    {
    [/font][font=Courier New][color=#941edf]public [/color][/font][font=Courier New][color=#941edf]static [/color][/font][font=Courier New][color=#941edf]void[/color][/font][font=Courier New] main (String[] args)
    {[/font][font=Courier New][color=#941edf]int[/color][/font][font=Courier New] num1, num2;
     
    System.out.println ([/font][font=Courier New][color=#00cb00]"Enter the first number "[/color][/font][font=Courier New]);
    num1 = Keyboard.readInt();
     
    System.out.println ([/font][font=Courier New][color=#00cb00]"Enter the second number "[/color][/font][font=Courier New]);
    num2 = Keyboard.readInt();
     
    [/font][font=Courier New][color=#941edf]static [/color][/font][font=Courier New][color=#941edf]int[/color][/font][font=Courier New] first ([/font][font=Courier New][color=#941edf]int[/color][/font][font=Courier New] num1)
    {[/font][font=Courier New][color=#941edf]int[/color][/font][font=Courier New] a;
    a = num1;
    [/font][font=Courier New][color=#941edf]return[/color][/font][font=Courier New] a;
    }
     
    [/font][font=Courier New][color=#941edf]static [/color][/font][font=Courier New][color=#941edf]int[/color][/font][font=Courier New] second ([/font][font=Courier New][color=#941edf]int[/color][/font][font=Courier New] num2)
    {[/font][font=Courier New][color=#941edf]int[/color][/font][font=Courier New] b;
    b = num2;
    [/font][font=Courier New][color=#941edf]return[/color][/font][font=Courier New] b;
    }
     
    [/font][font=Courier New][color=#941edf]static [/color][/font][font=Courier New][color=#941edf]int[/color][/font][font=Courier New] smaller ([/font][font=Courier New][color=#941edf]int[/color][/font][font=Courier New] a, [/font][font=Courier New][color=#941edf]int[/color][/font][font=Courier New] b)
    {
    [/font][font=Courier New][color=#941edf]if[/color][/font][font=Courier New] a < b;
     
    [/font][font=Courier New][color=#941edf]return[/color][/font][font=Courier New] a;
    }
     
    }
    }
     
     
    [/font]
     
  2. Zedric

    Zedric NTFS Guru Folding Team

    Messages:
    4,006
    Location:
    Sweden
    Yes...? What's the question?

    And what are the first two methods for? They do exactly nothing...

    Looking again... Why are the methods inside main? Does that even compile?
     
  3. PseudoKiller

    PseudoKiller Zug Zug

    Messages:
    3,858
    Location:
    Ice Crown Citadel
    this is what I need to do... (it was the first thing in the post)

    ignore the code... it is in pieces and dosent do anything but its the building blocks I have so far
     
  4. Geffy

    Geffy Moderator Folding Team

    Messages:
    7,805
    Location:
    United Kingdom
    I dont really know how your cs1.Keyboard import object works so I wrote it up how would have done

    PHP:
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.io.IOException;

    public class 
    BigSmall {
        
    // setup the keyboard input object so we can get stuff
        // from that keyboard thinger
        
    private static BufferedReader iKeyboard
                
    = new BufferedReader(new InputStreamReader(System.in));

        public static 
    void main(final String[] pArgsthrows IOException {
            
    int a first();
            
    int b second();
            
    int smaller smaller(a,b);
            
            
    System.out.println("The Smaller of the two you entered is: " smaller);
        }
        
        private static 
    int first() throws IOException {
            
    System.out.print("Enter first Number: ");
            
    System.out.flush();
            final 
    int first Integer.parseInt(iKeyboard.readLine());
            
            return 
    first;
        }
        
        private static 
    int second() throws IOException {
            
    System.out.print("Enter second Number: ");
            
    System.out.flush();
            final 
    int second Integer.parseInt(iKeyboard.readLine());
            
            return 
    second;
        }
        
        private static 
    int smaller(final int first, final int second) {
            if (
    first second) {
                return 
    first;    // If the first one is smaller then return
            
    } else {
                
    // Otherwise if the second one is smaller return it
                // also if they are the same then return the second
                // cause they are the same ;)
                
    return second;    
            }
        }
     
  5. Sazar

    Sazar F@H - Is it in you? Staff Member Political User Folding Team

    Messages:
    14,905
    Location:
    Between Austin and Tampa
    name your int's as whatever they are... keep in mind I am just giving you a basic gist of the coding..

    its also a little simpler than geffy's :D

    Code:
     import cs1.Keyboard;
    
    public class SmallNumber
    {
    //enter b.s. explanation here :)
    //
    public static void main (String[] args)
    {
    //declare ints
    int first, second, small = 0;
    
    System.out.println ("Enter 2 integers: ");
    
    first = Keyboard.readInt();
    second = Keyboard.readInt();
    
    if (first < second)
    small = first;
    else
    small = second;
    
    System.out.println ("The smallest value is : " + small);
    }
    }
    
     
  6. PseudoKiller

    PseudoKiller Zug Zug

    Messages:
    3,858
    Location:
    Ice Crown Citadel
    you guys are the best. I needed it 2 hours ago but the good thing is no one in class even started it. I am now hoping I can get some extra credit with it. WAHOO!?!
     
  7. Sazar

    Sazar F@H - Is it in you? Staff Member Political User Folding Team

    Messages:
    14,905
    Location:
    Between Austin and Tampa
    yeehah... go us...

    @ this rate pk's degree will belong to us :D
     
  8. PseudoKiller

    PseudoKiller Zug Zug

    Messages:
    3,858
    Location:
    Ice Crown Citadel
    well I am changing my major so unless you can do painting drawing and digital design, the degree will be mine... muhahahaha
     
  9. Sazar

    Sazar F@H - Is it in you? Staff Member Political User Folding Team

    Messages:
    14,905
    Location:
    Between Austin and Tampa
    :eek:

    you ungrateful @#$#@$!@# !!!!1

    :(
     
  10. PseudoKiller

    PseudoKiller Zug Zug

    Messages:
    3,858
    Location:
    Ice Crown Citadel
    yeah well my 2 year degree is now gonna take almost 3 years now. I am going into graphics and web design.
     
  11. Geffy

    Geffy Moderator Folding Team

    Messages:
    7,805
    Location:
    United Kingdom
    graphics and web design is more fun than Java I feel, I love doing web design as people can more easily appreciate the asthetic beauty in a nicely designed and usable website, but not many people can see beauty in Java code
     
  12. PseudoKiller

    PseudoKiller Zug Zug

    Messages:
    3,858
    Location:
    Ice Crown Citadel
    I used to do art work for a lot of years but gave it up when I found computers. It wasnt till recently I got a grip on PS7 and other graphics programs ... much more fun than Java and easier for me.
     
  13. X-Istence

    X-Istence * Political User

    Messages:
    6,498
    Location:
    USA
    Hrm, i like Geffy's better, it at least compiles and works with gjc, i dun know what cs1.importkeyboard.crap is.
     
  14. X-Istence

    X-Istence * Political User

    Messages:
    6,498
    Location:
    USA
    PHP:
     import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.io.IOException;

    public class 
    BigSmall {
        
    // setup the keyboard input object so we can get stuff
        // from that keyboard thinger
        
    private static BufferedReader iKeyboard
                
    = new BufferedReader(new InputStreamReader(System.in));

        public static 
    void main(final String[] pArgsthrows IOException {
            
            
    System.out.println("The Smaller of the two you entered is: " smaller(first(),second()));
        }
        
        private static 
    int first() throws IOException {
            
    System.out.print("Enter first Number: ");
            
    System.out.flush();
            
            return 
    Integer.parseInt(iKeyboard.readLine());
        }
        
        private static 
    int second() throws IOException {
            
    System.out.print("Enter second Number: ");
            
    System.out.flush();
                    
            return 
    Integer.parseInt(iKeyboard.readLine());
        }
        
        private static 
    int smaller(final int first, final int second) {
            if (
    first second) {
                return 
    first;    // If the first one is smaller then return
            
    } else {
                
    // Otherwise if the second one is smaller return it
                // also if they are the same then return the second
                // cause they are the same ;)
                
    return second;    
            }
        }
    Remove some excess memory allocations, untested, and it is basically Geffy's. Its my C++ instincts, do not allocate memory unless its absolutely needed.
     
  15. Geffy

    Geffy Moderator Folding Team

    Messages:
    7,805
    Location:
    United Kingdom
    yeah, bit neater that, the smaller() call looks slightly more confusing I can picture my Java Lecturer saying, but it does all compile and run on java sdk 1.4.2
     
  16. X-Istence

    X-Istence * Political User

    Messages:
    6,498
    Location:
    USA
    Doesnt look confusing at all. But then again i am used to nesting a lot of calls in one.
     
  17. Sazar

    Sazar F@H - Is it in you? Staff Member Political User Folding Team

    Messages:
    14,905
    Location:
    Between Austin and Tampa
    what aboot mine?

    it does the task w/o all teh crap...

    :(
     
  18. X-Istence

    X-Istence * Political User

    Messages:
    6,498
    Location:
    USA
    Geffy's is strict java, as in it will compile on any Java, yours uses some special keyboard library.
     
  19. Sazar

    Sazar F@H - Is it in you? Staff Member Political User Folding Team

    Messages:
    14,905
    Location:
    Between Austin and Tampa
    I used it since obviously pk is using it :)
     
  20. Geffy

    Geffy Moderator Folding Team

    Messages:
    7,805
    Location:
    United Kingdom
    I just dunno how that cs1.keyboard thinger works or how the code is setup so I tend to use my own, I have been thinking of rolling my own Keyboard, and so on classes though