AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

New To Java , I'm Stuck , Please Help!

 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Help
View previous topic :: View next topic  
Author Message
Guest






PostPosted: Thu Mar 23, 2006 3:12 pm    Post subject: New To Java , I'm Stuck , Please Help! Reply with quote



Hello,

I just started JAVA @ the university Level and as the semester is
coming to a close , the last assignment is proving to be tough for me
and I would greatly appeciate any help solving my problem. Anyways
this is the assignment,

"Write a program in JAVA that allows the user to input 5 ints. The
program will then output these five values in reverse order. The
program will then output the values in order with a statement on how
many of the remaining values are larger, smaller, or equal.

Example 1:
Input:
1
2
3
4
5

Output:
5
4
3
2
1
1 - 4 larger, 0 smaller, 0 equal
2 - 3 larger, 2 smaller, 0 equal
3 - 2 larger, 2 smaller, 0 equal
4 - 1 larger, 3 smaller, 0 equal
5 - 0 larger, 4 smaller, 0 equal
"

Ok so basically I have got everything up to the reverse order working ,
I even have the final loop set up which goes through the array and
formats the output to appear like this
"
[0] - 0 larger, 0 smaller, 0 equal
[1] - 0 larger, 0 smaller, 0 equal
[2] - 0 larger, 0 smaller, 0 equal
[3] - 0 larger, 0 smaller, 0 equal
[4]- 0 larger, 0 smaller, 0 equal

I can't figure out how to get the larger, smaller, equal part to work
and I've tried for many hours=S I'll post what I have so far.

import java.util.*;

public class ReverseOrder
{
public static void main (String[] args)
{

int larger = 0;
int smaller = 0;
int equals = 0;

Scanner scan = new Scanner (System.in);

int[] numbers = new int[5];
int[] check = new int[5];

for(int index=0; index < numbers.length; index++)
{
System.out.print ("Enter Number" + (index+1) + ":");
numbers[index] = scan.nextInt();
check[index] = numbers[index];
}



System.out.println ("The numbers in reverse order:");



for (int index=numbers.length-1; index >= 0; index--)
{
System.out.print (numbers[index] + " " + "\n");
}

for(int index=0; index < numbers.length; index++)
System.out.print (numbers[index] + " " + " Smaller " +
(smaller) + " Larger " + (larger) + " Equals " + (equals) + "\n");
}

}

Please let me know if you can help because I'm getting a major
headache! Thanks for your time.

Nick
Back to top
opalpa@gmail.com opalinsk
Guest





PostPosted: Thu Mar 23, 2006 3:12 pm    Post subject: Re: New To Java , I'm Stuck , Please Help! Reply with quote



Do you know how to create additional methods? Can you think of how you
can create a method that will, for a given number and an entire array,
tell you how many numbers are less than given number in entire array?
Analogusly can you make a method that, for a given number and an enitre
array, will tell you how many numbers are greater than given number in
entire array? Finally create a method for how many numbers are equal
to given number.

What is the check array for?

All the best, have fun,
Opalinski
opalpa (AT) gmail (DOT) com
http://www.geocities.com/opalpaweb/
Back to top
Rhino
Guest





PostPosted: Thu Mar 23, 2006 3:12 pm    Post subject: Re: New To Java , I'm Stuck , Please Help! Reply with quote



<nickdundas (AT) gmail (DOT) com> wrote in message
news:1143123241.920261.49000 (AT) g10g2000cwb (DOT) googlegroups.com...
Quote:
Hello,

I just started JAVA @ the university Level and as the semester is
coming to a close , the last assignment is proving to be tough for me
and I would greatly appeciate any help solving my problem. Anyways
this is the assignment,

"Write a program in JAVA that allows the user to input 5 ints. The
program will then output these five values in reverse order. The
program will then output the values in order with a statement on how
many of the remaining values are larger, smaller, or equal.

Example 1:
Input:
1
2
3
4
5

Output:
5
4
3
2
1
1 - 4 larger, 0 smaller, 0 equal
2 - 3 larger, 2 smaller, 0 equal
3 - 2 larger, 2 smaller, 0 equal
4 - 1 larger, 3 smaller, 0 equal
5 - 0 larger, 4 smaller, 0 equal
"

Ok so basically I have got everything up to the reverse order working ,
I even have the final loop set up which goes through the array and
formats the output to appear like this
"
[0] - 0 larger, 0 smaller, 0 equal
[1] - 0 larger, 0 smaller, 0 equal
[2] - 0 larger, 0 smaller, 0 equal
[3] - 0 larger, 0 smaller, 0 equal
[4]- 0 larger, 0 smaller, 0 equal

I can't figure out how to get the larger, smaller, equal part to work
and I've tried for many hours=S I'll post what I have so far.

import java.util.*;

public class ReverseOrder
{
public static void main (String[] args)
{

int larger = 0;
int smaller = 0;
int equals = 0;

Scanner scan = new Scanner (System.in);

int[] numbers = new int[5];
int[] check = new int[5];

for(int index=0; index < numbers.length; index++)
{
System.out.print ("Enter Number" + (index+1) + ":");
numbers[index] = scan.nextInt();
check[index] = numbers[index];
}



System.out.println ("The numbers in reverse order:");



for (int index=numbers.length-1; index >= 0; index--)
{
System.out.print (numbers[index] + " " + "\n");
}

for(int index=0; index < numbers.length; index++)
System.out.print (numbers[index] + " " + " Smaller " +
(smaller) + " Larger " + (larger) + " Equals " + (equals) + "\n");
}

}

Please let me know if you can help because I'm getting a major
headache! Thanks for your time.

I think many professional programmers would use the Collections classes to

help with this problem but I'm not sure if you are allowed to use this
approach; your instructor may feel that Collections are too advanced. There
is a decent chapter on the Collections classes and how to use them in the
Java Tutorial; you can find this chapter at
http://java.sun.com/docs/books/tutorial/collections/intro/index.html.

Without taking the time to solve your problem myself, I'm not sure which of
the Collections classes I would recommend for you to use. Certainly, the
basic job of storing your 5 inputs and sorting them would appear to be
easily handled by storing them in a List and using the sort() method but I
haven't though through the best technique for determining how many values
are higher/lower/equal to the individual inputs; you might want to transform
the List into a Map or SortedMap for that purpose.

--
Rhino
Back to top
Oliver Wong
Guest





PostPosted: Thu Mar 23, 2006 6:12 pm    Post subject: Re: New To Java , I'm Stuck , Please Help! Reply with quote

<nickdundas (AT) gmail (DOT) com> wrote in message
news:1143123241.920261.49000 (AT) g10g2000cwb (DOT) googlegroups.com...
Quote:
I just started JAVA @ the university Level and as the semester is
coming to a close , the last assignment is proving to be tough for me
and I would greatly appeciate any help solving my problem. Anyways
this is the assignment,

"Write a program in JAVA that allows the user to input 5 ints. The
program will then output these five values in reverse order. The
program will then output the values in order with a statement on how
many of the remaining values are larger, smaller, or equal.

Example 1:
Input:
1
2
3
4
5

Output:
5
4
3
2
1
1 - 4 larger, 0 smaller, 0 equal
2 - 3 larger, 2 smaller, 0 equal
3 - 2 larger, 2 smaller, 0 equal
4 - 1 larger, 3 smaller, 0 equal
5 - 0 larger, 4 smaller, 0 equal
"

Ok so basically I have got everything up to the reverse order working ,
I even have the final loop set up which goes through the array and
formats the output to appear

[...]

Quote:
I can't figure out how to get the larger, smaller, equal part to work
and I've tried for many hours=S I'll post what I have so far.

[code snipped]

Are you able to solve the problem yourself (i.e. if I didn't give you a
computer, but just gave you a sheet of paper with 5 numbers on them, could
you tell me, for each value, how many of the remaining values are larger,
smaller, or equal)?

If you can solve the problem, try to write down on a piece of paper how
you managed to solve that problem, in plain English. It might start off with
something like "Well, I looked at the first number, and then I looked at the
second number. If the second number was smaller, I'd make a mental note of
this, and then I..." etc.

Once you have that down, try to reformulate it as pseudo code, and then
eventually actual, legal Java code.

If you're still stuck, post as far as you could get with this
methodology (i.e. post your natural English instructions, and then your
pseudo-code if you managed to write any, and so on), and I'll try to help
you with the rest.

- Oliver
Back to top
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Help All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.