BestTechie Forums: Help With Java - BestTechie Forums

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Help With Java for class


#1 User is offline   Honda_Boy 

  • Resident Otaku
  • PipPipPipPipPipPip
  • Group: Members
  • Posts: 2630
  • Joined: 16-August 05
  • Location:Tennessee
  • Operating System:Windows 7 Professional x64, Windows Vista Home Premium x64

Posted 08 March 2008 - 09:13 PM

Well i was given this assignment and am without my book right now (should have it with me tomorrow). I don't remember the teacher going over this (this is one of few classes am actually awake all the way through). I hope somebody here can help me out:


Write a program that will prompt for the user to input an integer. Continue to take input until a sentinal
value is given. (I recommend a negative number) Once input is complete, print out the mean, min, and the max
of the inputted numbers.

You'll need to use an infinite loop and break it when you get the sentinal value. You will also need variables
for sum, mean, min, and max. Stick with the integer type throughout the program.

If any one can steer me in the right direction, I'd greatly appreciate it. Honestly I just don't remember ever remember going over anything dealing with user input at the command prompt.

* Ahhh, I do have something in my notes (my last class was 2 weeks ago, gimme a break) but it still doesn't make sense to me

Input java.util.scanner;
Scanner input = new scanner (System.in);
Int x;
X = input.next Int();

while(true)
{
	//take input
	If(sentinel_value== -1)
{
Break
}
//test to see if it’s the min
//test to see if it’s the max
//add it to the sum
//increment my counter
}
Print
Determine mean?


Now I was talking to my friend that's in the class with me over the break and he said he couldn't get his to work but that was as far as I got talkin with him.

This post has been edited by Honda_Boy: 08 March 2008 - 09:13 PM


#2 User is offline   Honda_Boy 

  • Resident Otaku
  • PipPipPipPipPipPip
  • Group: Members
  • Posts: 2630
  • Joined: 16-August 05
  • Location:Tennessee
  • Operating System:Windows 7 Professional x64, Windows Vista Home Premium x64

Posted 08 March 2008 - 09:47 PM

here's what I've written so far. Whether I'm even close to being on the right track or not, I don't know. I just know I'm getting 5 errors when trying to compile.

public class threems{
	public static void main(String Args[]){
		Java.util.scanner;
		int x;
		x = input.next;
		Int();
		while (true)
		{
			if(sentinel_value==-1)
		}
	}
}


it's getting three errors.

also, why it sucks to work with only one screen on this crap:

Posted Image

The only other system set up to program is my old Desktop that's in pieces right now cause of the new one. I got lazy and decided to work on the laptop. Big mistake.

#3 User is offline   jcl 

  • UberTechie
  • Group: Linux Experts
  • Posts: 1304
  • Joined: 30-August 04
  • Location:The Internet
  • Operating System:Arch

Posted 08 March 2008 - 11:18 PM

Your approach is correct but your syntax is a bit off. If that's what's giving you trouble... I'll have to think for a minute. Syntax is a pain until you internalize it.

You have my sympathy screen-wise.

Incidentally, bookmark or download the JDK documentation if you haven't done so yet. The API docs are worth their weight in gold. Take a look at the entry for java.util.Scanner, for example.

This post has been edited by jcl: 09 March 2008 - 04:23 AM


#4 User is offline   Honda_Boy 

  • Resident Otaku
  • PipPipPipPipPipPip
  • Group: Members
  • Posts: 2630
  • Joined: 16-August 05
  • Location:Tennessee
  • Operating System:Windows 7 Professional x64, Windows Vista Home Premium x64

Posted 09 March 2008 - 05:20 AM

thanks man!. I'll take look through that article and run through my notes again. I think I missed something in my notes so I'm gonna check them again.

#5 User is offline   Honda_Boy 

  • Resident Otaku
  • PipPipPipPipPipPip
  • Group: Members
  • Posts: 2630
  • Joined: 16-August 05
  • Location:Tennessee
  • Operating System:Windows 7 Professional x64, Windows Vista Home Premium x64

Posted 10 March 2008 - 08:35 PM

OK I know this much is right (or at least I think it is)

import java.util.Scanner;
public class threems{
	public static void main(String Args[]){
		Scanner input = new Scanner(System.in);


Now I need to make it ask for input then give me a Min, Max, and Mean from it. I'm just not sure how to do that.

#6 User is offline   Honda_Boy 

  • Resident Otaku
  • PipPipPipPipPipPip
  • Group: Members
  • Posts: 2630
  • Joined: 16-August 05
  • Location:Tennessee
  • Operating System:Windows 7 Professional x64, Windows Vista Home Premium x64

Posted 10 March 2008 - 09:17 PM

After finally locating a practice program similar to what I am trying to create, I came up with this code so far.

import java.util.Scanner;
public class threems{
	public static void main(String Args[]){
		Scanner input = new Scanner(System.in);
		int x;
		int sum;
		int xCounter;
		int mean;
		
		sum = 0;
		mean = 0;
		{
			System.out.print("Enter values to determine Max, Min, and Mean: ");}
		x = input.nextInt();
		while (x != -1);
		{
			sum = sum + x;
			xCounter = xCounter + 1;
			System.out.print("Enter next Value or -1 to quit: ");
			x = input.nextInt();
		}
		if (xCounter != 0)
		{
			mean = (int) sum / xCounter;
			System.out.printf("Mean = ", mean);
		}
	}
}


The problem is my xCounter right now. For some reason the xCounter = xCounter + 1 line won't work. It keeps saying xCounter may have not been initialized.

This post has been edited by Honda_Boy: 10 March 2008 - 09:23 PM


#7 User is offline   jcl 

  • UberTechie
  • Group: Linux Experts
  • Posts: 1304
  • Joined: 30-August 04
  • Location:The Internet
  • Operating System:Arch

Posted 10 March 2008 - 10:00 PM

View PostHonda_Boy, on Mar 11 2008, 01:17 AM, said:

The problem is my xCounter right now. For some reason the xCounter = xCounter + 1 line won't work. It keeps saying xCounter may have not been initialized.


You need to provide an initial value to xCounter a la sum and mean. (Local variables must be explicitly given a value before they can be used.)

This post has been edited by jcl: 10 March 2008 - 10:10 PM


#8 User is offline   Honda_Boy 

  • Resident Otaku
  • PipPipPipPipPipPip
  • Group: Members
  • Posts: 2630
  • Joined: 16-August 05
  • Location:Tennessee
  • Operating System:Windows 7 Professional x64, Windows Vista Home Premium x64

Posted 10 March 2008 - 10:09 PM

import java.util.Scanner;
public class threems{
	public static void main(String Args[]){
		Scanner input = new Scanner(System.in);
		int x;
		int sum;
		int mean;
		int xCounter;
		sum = 0;
		xCounter = 0;
		
		System.out.print("Enter values to determine Max, Min, and Mean: ");
		x = input.nextInt();
		while (x != -1)
		{
			sum = sum + x;
			xCounter = xCounter + 1;
			System.out.print("Enter next Value or -1 to quit: ");
			x = input.nextInt();
		}
		if (xCounter != 0)
		{
			mean = sum / xCounter;
			System.out.print("Mean: ");
			System.out.print(mean);
		}
		else 
			System.out.println("No Values were entered");
	}
}


Alright I got it to at least do the Mean but I don't have a clue how to do Max and min.

and yeah i figured out the xCounter on my own. I just forgot to change something from earlier. That was just stupidity on my part.

This post has been edited by Honda_Boy: 10 March 2008 - 10:10 PM


#9 User is offline   jcl 

  • UberTechie
  • Group: Linux Experts
  • Posts: 1304
  • Joined: 30-August 04
  • Location:The Internet
  • Operating System:Arch

Posted 10 March 2008 - 10:19 PM

View PostHonda_Boy, on Mar 11 2008, 02:09 AM, said:

Alright I got it to at least do the Mean but I don't have a clue how to do Max and min.


The outline in your first post has the answer: test each input against the current min and max and update them if needed. The only tricky bit is picking the initial values for min and max.

This post has been edited by jcl: 10 March 2008 - 10:19 PM


#10 User is offline   Honda_Boy 

  • Resident Otaku
  • PipPipPipPipPipPip
  • Group: Members
  • Posts: 2630
  • Joined: 16-August 05
  • Location:Tennessee
  • Operating System:Windows 7 Professional x64, Windows Vista Home Premium x64

Posted 10 March 2008 - 10:34 PM

View Postjcl, on Mar 10 2008, 10:19 PM, said:

The outline in your first post has the answer: test each input against the current min and max and update them if needed. The only tricky bit is picking the initial values for min and max.


OK, something from my last class popped up in my head. I remember him talking about that, I just can't remember how to test them. I think I forgot to write it down (damn ADD.....and 3 hour long class).

#11 User is offline   Honda_Boy 

  • Resident Otaku
  • PipPipPipPipPipPip
  • Group: Members
  • Posts: 2630
  • Joined: 16-August 05
  • Location:Tennessee
  • Operating System:Windows 7 Professional x64, Windows Vista Home Premium x64

Posted 11 March 2008 - 10:43 AM

Finally got it working:

import java.util.Scanner;
public class threems{
	public static void main(String Args[]){
		Scanner input = new Scanner(System.in);
		int x;
		int sum = 0;
		int mean;
		int xCounter = 0;
		int max = 0;
		int min = 10000;

		System.out.print("Enter values to determine Max, Min, and Mean: ");
		x = input.nextInt();
		while (x != -1)
		{
			sum = sum + x;
			xCounter = xCounter + 1;
			while (x > max)
			{
				max = x;
			}
			while (x < min)
			{
				min = x;
			}
			System.out.print("Enter next Value or -1 to quit: ");
			x = input.nextInt();
		}
		if (xCounter != 0)
		{
			mean = sum / xCounter;
			System.out.println("Mean: " + mean + " Max: " + max + " Min: " + min);
		}
		else
		{	
			System.out.println("No Values were entered");
		}
	}
}


#12 User is offline   iccaros 

  • UberTechie
  • Group: Linux Experts
  • Posts: 1292
  • Joined: 31-August 04
  • Location:Great State of Washingtion
  • Operating System:Gentoo,Iccaros-Linux(of course),Slackware,GentooX,Red Hat, Windows (3.1 to VISTA BETA), MAC OSX (10.4 currently),LFS, Solaris 8,9,10, Trusted Solaris, FreeBSD, OPENBSD, NETBSD

Posted 13 March 2008 - 04:16 AM

some notes that don't fix your problem but may help in the future.

first I never use magic numbers (that is I never test values against a number explisitly, this way you can change the test condition latter with a configuration file when the code is more complex.

example

int sentinel = -1;


while (x != sentinel)

this way in larger code projects the test case also means more to you, after a while you may not remember what -1 is.

also you could change this to

int sentinel = 0;

while (x > sentinel)

this way any negative number would end the loop



another thing


you can use += instead of item = item + x;
so it would be item += x;

and to increment a value you can use ++;
so item = item + 1 can be written
item++;

the same as item = item -1 could be
item--;


and why do you have while statements to test the value of min or max? should those not be if statements?

if(x > max)
{
max = x;
}
else if (x < min)
{
min = x;
}


and should the input be inside your while statement?

you have this
System.out.print("Enter values to determine Max, Min, and Mean: ");
x = input.nextInt();

twice when it could be once if it came after your while statement

while (x != -1)


I would also change that to a boolean value and test x after the input and make the value true


example

sentinel = 0;
boolean test = true;


while(test)
{
System.out.print("Enter values to determine Max, Min, and Mean: ");
x = input.nextInt();
if (x < sentinel)
{
test = false;
}

//do rest of work
}

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users