Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / March 2006

Tip: Looking for answers? Try searching our database.

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

Thread view: 
nickdundas@gmail.com - 23 Mar 2006 15:14 GMT
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
opalpa@gmail.com opalinski from opalpaweb - 23 Mar 2006 15:28 GMT
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@gmail.com
http://www.geocities.com/opalpaweb/
Rhino - 23 Mar 2006 15:31 GMT
> Hello,
>
[quoted text clipped - 81 lines]
> 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
Oliver Wong - 23 Mar 2006 18:51 GMT
> 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
[quoted text clipped - 30 lines]
> I even have the final loop set up which goes through the array and
> formats the output  to appear

[...]

> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.