Java Methods

P

PseudoKiller

Guest
#1
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
Jan 12, 2002
Messages
4,006
#2
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
#3
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
Mar 18, 2002
Messages
7,805
#4
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

F@H - Is it in you?
Staff member
Political User
Joined
Apr 12, 2002
Messages
14,905
#5
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);
}
}
 
P

PseudoKiller

Guest
#6
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

F@H - Is it in you?
Staff member
Political User
Joined
Apr 12, 2002
Messages
14,905
#7
yeehah... go us...

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

PseudoKiller

Guest
#8
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
#10
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
Mar 18, 2002
Messages
7,805
#11
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
#12
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 User
Joined
Dec 5, 2001
Messages
6,498
#13
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 User
Joined
Dec 5, 2001
Messages
6,498
#14
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
Mar 18, 2002
Messages
7,805
#15
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
 

Sazar

F@H - Is it in you?
Staff member
Political User
Joined
Apr 12, 2002
Messages
14,905
#17
what aboot mine?

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

:(
 

Sazar

F@H - Is it in you?
Staff member
Political User
Joined
Apr 12, 2002
Messages
14,905
#19
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
Mar 18, 2002
Messages
7,805
#20
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 posts

Latest profile posts

Perris Calderon wrote on Electronic Punk's profile.
Ep, glad to see you come back and tidy up...did want to ask a one day favor, I want to enhance my resume , was hoping you could make me administrator for a day, if so, take me right off since I won't be here to do anything, and don't know the slightest about the board, but it would be nice putting "served administrator osnn", if can do, THANKS

Been running around Quora lately, luv it there https://tinyurl.com/ycpxl
Electronic Punk wrote on Perris Calderon's profile.
All good still mate?
Hello, is there anybody in there? Just nod if you can hear me ...
Xie
What a long strange trip it's been. =)

Forum statistics

Threads
62,000
Messages
673,427
Members
89,025
Latest member
Haniew