Reply
Old March 29th, 2004 Top | #1
 
PseudoKiller's Avatar
Zug Zug
Joined: April 2002
Location: Ice Crown Citadel
Posts: 3,858
Reputation: 490
Power: 162

Default Java Methods

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:

import cs1.Keyboard; //this is used for Keyboard inputs
 
public class BigSmall
 
{
public static void main (String[] args)
{int num1, num2;
 
System.out.println ("Enter the first number ");
num1 = Keyboard.readInt();
 
System.out.println ("Enter the second number ");
num2 = Keyboard.readInt();
 
static int first (int num1)
{int a;
a = num1;
return a;
}
 
static int second (int num2)
{int b;
b = num2;
return b;
}
 
static int smaller (int a, int b)
{
if a < b;
 
return a;
}
 
}
}
 
 

)|( I reject your reality and substitute my own )|(
PseudoKiller is offline   Reply With Quote
Old March 29th, 2004 Top | #2

OSNN Folding Team  
Zedric's Avatar
NTFS Guru
Joined: January 2002
Location: Sweden
Posts: 4,006
Reputation: 890
Power: 171

Cool

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?


Did I help you? Please use the reputation system. Click the icon on the left!
Proud host of the OSNN.net folding sigs. Want one? Check the folding thread!
http://zedric.no-ip.com/
Zedric is offline   Reply With Quote
Old March 29th, 2004 Top | #3
 
PseudoKiller's Avatar
Zug Zug
Joined: April 2002
Location: Ice Crown Citadel
Posts: 3,858
Reputation: 490
Power: 162

Default

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 reject your reality and substitute my own )|(
PseudoKiller is offline   Reply With Quote
Old March 30th, 2004 Top | #4

OSNN Folding Team  
Geffy's Avatar
OSNN Veteran Addict
Joined: March 2002
Location: United Kingdom
Posts: 7,805
Reputation: 1490
Power: 213

Default

I dont really know how your cs1.Keyboard import object works so I wrote it up how would have done

PHP Code:
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;    
        }
    }



blogtumbloglastfmflickr#rubyonrails@twitter
"I could be replaced with a very small shell script"
Geffy is offline   Reply With Quote
Old March 30th, 2004 Top | #5

OSNN Folding Team  
Sazar's Avatar
F@H - Is it in you?
Joined: April 2002
Location: Between Austin and Tampa
Posts: 14,880
Reputation: 4110
Power: 309

Default

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);
}
}
Sazar is offline   Reply With Quote
Old March 30th, 2004 Top | #6
 
PseudoKiller's Avatar
Zug Zug
Joined: April 2002
Location: Ice Crown Citadel
Posts: 3,858
Reputation: 490
Power: 162

Default

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

)|( I reject your reality and substitute my own )|(
PseudoKiller is offline   Reply With Quote
Old March 30th, 2004 Top | #7

OSNN Folding Team  
Sazar's Avatar
F@H - Is it in you?
Joined: April 2002
Location: Between Austin and Tampa
Posts: 14,880
Reputation: 4110
Power: 309

Default

yeehah... go us...

@ this rate pk's degree will belong to us
Sazar is offline   Reply With Quote
Old March 30th, 2004 Top | #8
 
PseudoKiller's Avatar
Zug Zug
Joined: April 2002
Location: Ice Crown Citadel
Posts: 3,858
Reputation: 490
Power: 162

Default

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

)|( I reject your reality and substitute my own )|(
PseudoKiller is offline   Reply With Quote
Old March 30th, 2004 Top | #9

OSNN Folding Team  
Sazar's Avatar
F@H - Is it in you?
Joined: April 2002
Location: Between Austin and Tampa
Posts: 14,880
Reputation: 4110
Power: 309

Default



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

Sazar is offline   Reply With Quote
Old March 30th, 2004 Top | #10
 
PseudoKiller's Avatar
Zug Zug
Joined: April 2002
Location: Ice Crown Citadel
Posts: 3,858
Reputation: 490
Power: 162

Default

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

)|( I reject your reality and substitute my own )|(
PseudoKiller is offline   Reply With Quote
Old March 30th, 2004 Top | #11

OSNN Folding Team  
Geffy's Avatar
OSNN Veteran Addict
Joined: March 2002
Location: United Kingdom
Posts: 7,805
Reputation: 1490
Power: 213

Default

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


blogtumbloglastfmflickr#rubyonrails@twitter
"I could be replaced with a very small shell script"
Geffy is offline   Reply With Quote
Old March 30th, 2004 Top | #12
 
PseudoKiller's Avatar
Zug Zug
Joined: April 2002
Location: Ice Crown Citadel
Posts: 3,858
Reputation: 490
Power: 162

Default

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.

)|( I reject your reality and substitute my own )|(
PseudoKiller is offline   Reply With Quote
Old March 30th, 2004 Top | #13
 
X-Istence's Avatar
*
Joined: December 2001
Location: USA
Posts: 6,490
Reputation: 2808
Power: 217

Default

Hrm, i like Geffy's better, it at least compiles and works with gjc, i dun know what cs1.importkeyboard.crap is.
X-Istence is offline   Reply With Quote
Old March 30th, 2004 Top | #14
 
X-Istence's Avatar
*
Joined: December 2001
Location: USA
Posts: 6,490
Reputation: 2808
Power: 217

Default

PHP Code:
 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.
X-Istence is offline   Reply With Quote
Old March 30th, 2004 Top | #15

OSNN Folding Team  
Geffy's Avatar
OSNN Veteran Addict
Joined: March 2002
Location: United Kingdom
Posts: 7,805
Reputation: 1490
Power: 213

Default

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


blogtumbloglastfmflickr#rubyonrails@twitter
"I could be replaced with a very small shell script"
Geffy is offline   Reply With Quote
Old March 30th, 2004 Top | #16
 
X-Istence's Avatar
*
Joined: December 2001
Location: USA
Posts: 6,490
Reputation: 2808
Power: 217

Default

Doesnt look confusing at all. But then again i am used to nesting a lot of calls in one.
X-Istence is offline   Reply With Quote
Old March 30th, 2004 Top | #17

OSNN Folding Team  
Sazar's Avatar
F@H - Is it in you?
Joined: April 2002
Location: Between Austin and Tampa
Posts: 14,880
Reputation: 4110
Power: 309

Default

what aboot mine?

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

Sazar is offline   Reply With Quote
Old March 31st, 2004 Top | #18
 
X-Istence's Avatar
*
Joined: December 2001
Location: USA
Posts: 6,490
Reputation: 2808
Power: 217

Default

Geffy's is strict java, as in it will compile on any Java, yours uses some special keyboard library.
X-Istence is offline   Reply With Quote
Old March 31st, 2004 Top | #19

OSNN Folding Team  
Sazar's Avatar
F@H - Is it in you?
Joined: April 2002
Location: Between Austin and Tampa
Posts: 14,880
Reputation: 4110
Power: 309

Default

Originally Posted by X-Istence
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
Sazar is offline   Reply With Quote
Old March 31st, 2004 Top | #20

OSNN Folding Team  
Geffy's Avatar
OSNN Veteran Addict
Joined: March 2002
Location: United Kingdom
Posts: 7,805
Reputation: 1490
Power: 213

Default

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


blogtumbloglastfmflickr#rubyonrails@twitter
"I could be replaced with a very small shell script"
Geffy is offline   Reply With Quote

Reply

Bookmarks

Thread Tools

Posting Rules

Similar Threads
Thread Thread Starter Forum Replies Last Post
Overriding Methods in Visual Studio 2005 Complete Web Design & Coding 0 October 10th, 2007 8:32pm
Methods before Members or Members before Methods? Complete Web Design & Coding 2 September 19th, 2007 11:15am
methods for WBXML decoding sachinjain_30 Portable Devices & Gadgets 0 October 5th, 2006 5:17pm
Backup methods? jkoXP Windows Desktop Systems 5 September 16th, 2003 11:51pm
Other Methods With Ramdisk!?? Hercules Windows Desktop Systems 2 April 14th, 2002 12:13am