Java Methods

  • Thread starter Thread starter PseudoKiller
  • Start date Start date
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]
 

Zedric

NTFS Guru
Joined
12 Jan 2002
Messages
4,006
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?
 
P

PseudoKiller

Guest
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
 

Geffy

OSNN Veteran Addict
Joined
18 Mar 2002
Messages
7,805
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;	
		}
	}
}
 

Sazar

Rest In Peace
Joined
12 Apr 2002
Messages
14,905
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 😀

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

PseudoKiller

Guest
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!?!
 

Sazar

Rest In Peace
Joined
12 Apr 2002
Messages
14,905
yeehah... go us...

@ this rate pk's degree will belong to us 😀
 
P

PseudoKiller

Guest
well I am changing my major so unless you can do painting drawing and digital design, the degree will be mine... muhahahaha
 
P

PseudoKiller

Guest
yeah well my 2 year degree is now gonna take almost 3 years now. I am going into graphics and web design.
 

Geffy

OSNN Veteran Addict
Joined
18 Mar 2002
Messages
7,805
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
 
P

PseudoKiller

Guest
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.
 

X-Istence

*
Political Access
Joined
5 Dec 2001
Messages
6,498
Hrm, i like Geffy's better, it at least compiles and works with gjc, i dun know what cs1.importkeyboard.crap is.
 

X-Istence

*
Political Access
Joined
5 Dec 2001
Messages
6,498
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.
 

Geffy

OSNN Veteran Addict
Joined
18 Mar 2002
Messages
7,805
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
 

X-Istence

*
Political Access
Joined
5 Dec 2001
Messages
6,498
Doesnt look confusing at all. But then again i am used to nesting a lot of calls in one.
 

Sazar

Rest In Peace
Joined
12 Apr 2002
Messages
14,905
what aboot mine?

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

🙁
 

X-Istence

*
Political Access
Joined
5 Dec 2001
Messages
6,498
Geffy's is strict java, as in it will compile on any Java, yours uses some special keyboard library.
 

Sazar

Rest In Peace
Joined
12 Apr 2002
Messages
14,905
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 🙂
 

Geffy

OSNN Veteran Addict
Joined
18 Mar 2002
Messages
7,805
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

Xie Electronic Punk 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 Sazar 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.
Terrahertz Electronic Punk Terrahertz wrote on Electronic Punk's profile.
Yo fellas!
Electronic Punk Sazar Electronic Punk wrote on Sazar's profile.
Where are you buddy?

Forum statistics

Threads
62,017
Messages
673,508
Members
5,636
Latest member
GLOCKTOR642
Back