Java Methods

P

PseudoKiller

Guest
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]
 
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?
 
this is what I need to do... (it was the first thing in the post)

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.

ignore the code... it is in pieces and dosent do anything but its the building blocks I have so far
 
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[] pArgs) throws 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;	
		}
	}
}
 
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);
}
}
 
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!?!
 
yeehah... go us...

@ this rate pk's degree will belong to us :D
 
well I am changing my major so unless you can do painting drawing and digital design, the degree will be mine... muhahahaha
 
yeah well my 2 year degree is now gonna take almost 3 years now. I am going into graphics and web design.
 
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
 
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.
 
Hrm, i like Geffy's better, it at least compiles and works with gjc, i dun know what cs1.importkeyboard.crap is.
 
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[] pArgs) throws 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.
 
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
 
Doesnt look confusing at all. But then again i am used to nesting a lot of calls in one.
 
what aboot mine?

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

:(
 
Geffy's is strict java, as in it will compile on any Java, yours uses some special keyboard library.
 
X-Istence said:
Geffy's is strict java, as in it will compile on any Java, yours uses some special keyboard library.

I used it since obviously pk is using it :)
 
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
 

Members online

No members online now.

Latest profile posts

Also Hi EP and people. I found this place again while looking through a oooollllllldddd backup. I have filled over 10TB and was looking at my collection of antiques. Any bids on the 500Mhz Win 95 fix?
Any of the SP crew still out there?
Xie wrote on Electronic Punk's profile.
Impressed you have kept this alive this long EP! So many sites have come and gone. :(

Just did some crude math and I apparently joined almost 18yrs ago, how is that possible???
hello peeps... is been some time since i last came here.
Electronic Punk wrote on Sazar's profile.
Rest in peace my friend, been trying to find you and finally did in the worst way imaginable.

Forum statistics

Threads
62,015
Messages
673,494
Members
5,621
Latest member
naeemsafi
Back