There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
Search
 
Software Development
Tag Cloud
adware audio bios blue screen boot bsod computer crash dell desktop driver drivers email error excel firefox freeze google hard drive hardware hijackthis install internet itunes laptop linux malware network no sound outlook problem recovery router screen slow sound speakers spyware startup trojan usb video virus vista webcam windows windows 7 windows vista windows xp wireless
Search
Search for:
Tech Support Guy Forums > Software & Hardware > Software Development >
More Java beginner's help

Tip: Click here to scan for System Errors and Optimize PC performance
[ Sponsored Link ]

Closed Thread
 
Thread Tools
CaseyRenee87's Avatar
Member with 31 posts.
 
Join Date: May 2009
Location: oklahoma
Experience: Intermediate
05-Jul-2009, 04:16 PM #1
Exclamation More Java beginner's help
hey everyone, I have another assignment i am working on, and I am a little stumped..not as bad as last time but I need a little help with writing a program.
anyways here is some code i have written so far, this assignment is meant to input 10 integers from a user and then determine the largest of those 10. feel free to correct any "bugs" i may have written.
//LargerInteger.java
import java.util.Scanner

public class LargerInteger
{
public void determineLargerInteger()
{
scanner input-new Scanner(System.in);

int largest; //the largest number found so far
int numberCounter;//counts to 10 and keeps track of numbers entered so far
int number;//variable stores each integer entered by user

Largest =0;
numberCounter=1;
//while loop/nested If statements?
while (numberCOunter <=10)//loop 10 times
{
System.out.print("Enter first integer: ");
number= input.nextInt();....//I think this continues like this for each integer input, though I am not completely sure, because with while loops you can write minimal code to perform extensive tasks..i am just not sure how to go about it with this particular program.
..}//end while loop
How would I edit the while loop with nested if or if...else statements to input 10 integers and determine and print the largest those 10 integers?
are there any special timesaving tips i can use to accomplish this task?
I am kind of behind on my assignments trying to figure this one out. I got it in the back of my mind i am just kind of stumped on a few areas.
thanks in advance for your help!
DoubleHelix's Avatar
Distinguished Member with 12,627 posts.
 
Join Date: Dec 2004
Experience: A little of this...a little of that
05-Jul-2009, 06:05 PM #2
You can't determine the largest number until they've all been entered. That functionality needs to be handled outside the input loop.

There must have been a bit more in the lesson to help you with this. You haven't written any code for sorting or searching.
CaseyRenee87's Avatar
Member with 31 posts.
 
Join Date: May 2009
Location: oklahoma
Experience: Intermediate
05-Jul-2009, 09:21 PM #3
Ohhh okay, hmmm I can post the whole lesson assignment directions here, the numbers that are supposed to be entered are random numbers and could be any integer. here is the problem:

The process of finding the largest value (ie the maximum in a group of values) is used frequently in computer appliactions. for example a program that determines the winner of a sales contest would input the number of units sold by each salesperson. The person who sells the most units wins the contest. write a pseudocode program(I have written half of one so far) and then a Java application that inputs a series of 10 integers and determines and prints the largest integer.
the program should include at least the following three variables:
counter= to keep track of how many numbers have been input and to count to 10
number: the integer most recently input by the user
largest: the largest number found so far

and here is some of the pseudocode i have written so far:
initialize variables

while number is less than ten
prompt user to enter the next number
input next number
add number to the total
compare number with all recently input integers to determine the largest so far
print the largest integer


that's the basic pseudocode i have written out to help me accomplish the task, I am just unsure of what steps to take to input 10 integers then determine the largest so far.
DoubleHelix's Avatar
Distinguished Member with 12,627 posts.
 
Join Date: Dec 2004
Experience: A little of this...a little of that
05-Jul-2009, 10:40 PM #4
You're unsure about the crux of the assignment. Do you have an instructor? A tutor? These are basic programming operations. You have to work through this on your own if you want to learn.
CaseyRenee87's Avatar
Member with 31 posts.
 
Join Date: May 2009
Location: oklahoma
Experience: Intermediate
06-Jul-2009, 04:45 AM #5
Yes actually, I do though this is a distance learning course..so I have to email them. Anyways I will try to work it out tomorrow..and I'll come back here if I have bugs in my code, because those are the hardest to figure out. I'm also in kind of a hurry because this course expires July 30th, and if i dont complete it by then i have to buy another extension. anyways i will try and work it out on my own and come back here if i shall have a problem.

thanks!
burnthepc's Avatar
Computer Specs
Senior Member with 305 posts.
 
Join Date: Aug 2007
Location: London, UK
Experience: Intermediate
06-Jul-2009, 08:59 PM #6
Quote:
Originally Posted by CaseyRenee87 View Post
compare number with all recently input integers to determine the largest so far
print the largest integer
Why compare all input to determine the largest?

You have two vairiables there: largest and number.

Largest is the biggest number so far, number is the current input.

so

if largest is less than than number,
then largest should be the current input.
NeonFx's Avatar
Senior Member with 1,432 posts.
 
Join Date: Oct 2008
Location: California, USA
06-Jul-2009, 11:00 PM #7
Quote:
Originally Posted by burnthepc
if largest is less than than number,
then largest should be the current input.
exactly

int inputed = 0;
int highest;

while(inputed < 10){
//ask for int and assign int to current
if(current > highest) highest = current;
total += current;
inputed++;
}
CaseyRenee87's Avatar
Member with 31 posts.
 
Join Date: May 2009
Location: oklahoma
Experience: Intermediate
07-Jul-2009, 12:54 AM #8
Thanks neonfx and burnthepc!!!
that's exactly what I was needing to know I will write up the code, and try it out some and if i run into problems..i will come back here with them.
CaseyRenee87's Avatar
Member with 31 posts.
 
Join Date: May 2009
Location: oklahoma
Experience: Intermediate
07-Jul-2009, 08:09 PM #9
here is some code I have gotten done so far, using the tips you guys submitted. there seems to be a few bugs in it though..one error says it cannot access LargestValue because it is a non static class.. what could be causing that error message?

also: I havent yet entered a termination phase..I am not sure what to put.

//LargestValue.java Assignment 6946
//casey clark student no.:
import java.util.Scanner; //scanner class import declaration
class LargestValue
{
public void determineLargestValue()
{
//create Scanner to obtain input from command window
Scanner input = new Scanner(System.in);

int total;
int intCounter;
int number=0;//currently input number
int largest;//largest number found so far

//initialization
total=0;
intCounter=1;
largest=0;

while (number<10){
System.out.print("Enter number: ");
number = input.nextInt();
total= total + number;
intCounter = intCounter ++;

if (number>largest)
largest=number;
total += number;
intCounter++;

}//end while

//termination phase

//display total
System.out.printf("\nThe largest of all 10 integers is:%d\n", largest);
}//end method LargestValue
}//end class
//LargestValueTest.java
//invoke the determine largest value method in LargestValue class
public class LargestValueTest{
public void main(String args[])
{
LargestValue.determineLargestValue();//find the largest integer in 10
}//end main
}//end class
burnthepc's Avatar
Computer Specs
Senior Member with 305 posts.
 
Join Date: Aug 2007
Location: London, UK
Experience: Intermediate
07-Jul-2009, 08:45 PM #10
That error is because you haven't created an object.
LargestValue.determineLargestValue() is trying to call a static version of the method.

Don't worry yourself over static methods (although that's a better solution to the problem). Just create a vairiable for your largest value class and call the method on that
NeonFx's Avatar
Senior Member with 1,432 posts.
 
Join Date: Oct 2008
Location: California, USA
07-Jul-2009, 08:57 PM #11
Use burnthepc's advice to edit the main class

Here's a few things you need to learn before you get the LargestValue class right:
  1. Go over your loop; you seem to have misunderstood how it works. You want to do something over and over again as long as the number of times you do that something is less than 10.
  2. If you are using an if statement over more than one line you will need brackets around the code
  3. total += number; is the same thing as saying total = total + number;
  4. intCounter++ is the same thing as intCounter = intCounter + 1;
Considering these, how would you change your code?
__________________
Please post the final results, good or bad. Let me know if you won't be responding any longer.
If I have not responded in three days, please feel free to PM me with a friendly reminder.
Please don't send me requests for help. Use the forums instead.
CaseyRenee87's Avatar
Member with 31 posts.
 
Join Date: May 2009
Location: oklahoma
Experience: Intermediate
09-Jul-2009, 04:08 AM #12
ok guys I edited my code...it seems to look right, or match the other class example in my textbook..but when I try and execute it, it gives the "No such Method Java.Lang" error. so we've moved from a compile time error to a fatal runtime error.


//LargestValue.java Assignment 6946
//casey clark student no.: MA2008030031
import java.util.Scanner; //scanner class import declaration
public class LargestValue
{
public void determineLargestValue()
{
//create Scanner to obtain input from command window
Scanner input = new Scanner(System.in);

int total;
int intCounter;
int number=0;//currently input number
int largest;//largest number found so far

//initialization
total=0;
intCounter=1;
largest=0;

while (number<10)
{
System.out.print("Enter number: ");
number = input.nextInt();
total+= number;
intCounter ++;

if
(number>largest)
{
largest=number;
total += number;
intCounter++;
}//end if

}//end while

//termination phase

//display total
System.out.printf("\nThe Total is: %d\n", total);
System.out.printf("\nThe largest of all 10 integers is: %d\n", largest);
}//end method LargestValue
}//end class


class LargestValueTest
{
public void main(String args[])
{
LargestValue LargestValue2 = new LargestValue();//create object of LargestValue class to call method
LargestValue2.determineLargestValue();//find the largest integer in 10
}//end main
}//end class
NeonFx's Avatar
Senior Member with 1,432 posts.
 
Join Date: Oct 2008
Location: California, USA
09-Jul-2009, 01:20 PM #13
Hi there.

Change

Quote:
public void main(String args[])
to this:

public static void main(String args[])


Your program should compile and run just fine after that.

When you run your program, try entering the number 10, see what happens.

The reason your program stops asking for numbers there has to do with:

Quote:
while (number<10)
What is wrong with this line? How do "while loops" work?

If you can't answer that, try creating a different kind of loop you might be more familiar with.
__________________
Please post the final results, good or bad. Let me know if you won't be responding any longer.
If I have not responded in three days, please feel free to PM me with a friendly reminder.
Please don't send me requests for help. Use the forums instead.
CaseyRenee87's Avatar
Member with 31 posts.
 
Join Date: May 2009
Location: oklahoma
Experience: Intermediate
09-Jul-2009, 08:21 PM #14
Ah!!! ok i changed that method main back to a static method...i forgot i had changed it because of that previous error.

ok..now the while loop is what is giving me the most trouble LOL..I am new to While loops, plus they are the only loop i have started to learn so far, and not to mention this program requires Iteration ..actually this lesson is teaching beginner control statements.

I found out though my program DOES have a logic error. it has a sort of Infinite Loop. I changed the < to =<, and now it keeps asking for integers AS LONG as they are single digit. now if they are double or more digits it stops asking for them and outputs the total..and largest. It dosent help that i am kind of bad at math.
NeonFx's Avatar
Senior Member with 1,432 posts.
 
Join Date: Oct 2008
Location: California, USA
09-Jul-2009, 08:26 PM #15
It is only infinite as long as you don't enter a number 10 or above

Why is that? because you are checking the state of the wrong control value.

The control value for the while loop should be whatever you increment by one every time the code runs.

For example, the control value for the while loop is count. This will print "Hello World!" ten times.

while(count < 10)
{
System.out.println("Hello World!");
count++;
}


you are checking if number is less than 10 where you should be checking if IntCounter is less than ten IntCounter should only increment by one every time the loop is run.
__________________
Please post the final results, good or bad. Let me know if you won't be responding any longer.
If I have not responded in three days, please feel free to PM me with a friendly reminder.
Please don't send me requests for help. Use the forums instead.
Closed Thread Bookmark and Share

THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.

Smart Search

Find your solution!



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who want to help you solve your computer problems. See our Welcome Guide to get started.

Thread Tools


You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -5. The time now is 07:10 PM.
Copyright © 1996 - 2009 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.
Powered by Cermak Technologies, Inc.