im new and need help

im new and need help with my "homework"

Page 1 of 1

9 Replies - 968 Views - Last Post: 22 June 2009 - 07:29 AM Rate Topic: -----

#1 Java_noob  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 21-June 09

im new and need help

Posted 21 June 2009 - 11:08 AM

i need to write a program which generates 3 random numbers called tilfældigtal1, tilfældigtal2 and tilfældigtal 3.

with these 3 numbers it must find the sum and the mean number (the number you find, when you find the sum of the 3 numbers and / it with 3) .

my code looks like this, but i dont know whats wrong with it.

public class sum_and_mean_number {

public static void main(String[] args) {

double tilfældigtal1, tilfældigtal2, tilfældigtal3;
tilfældigtal1 = Math.random();
tilfældigtal2 = Math.random();
tilfældigtal3 = Math.random();

System.out.println( "1 tal er"+tilfældigtal1", 2 tal er"+tilfældigtal2" og 3 tal er "+tilfældigtal3" );

double Mean, Sum;
Mean = ((tilfældigtal1 + tilfældigtal2 + tilfældigtal3)/3);
Sum = (tilfældigtal1 + tilfældigtal2 + tilfældigtal3);

System.out.println("sum is "+Sum" og Mean is "+Mean);
}
}

can you plz explain where i did something wrong, and why it was.

since i must know that, so i dont make a mistake again

This post has been edited by Java_noob: 21 June 2009 - 11:09 AM


Is This A Good Question/Topic? 0
  • +

Replies To: im new and need help

#2 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: im new and need help

Posted 21 June 2009 - 11:23 AM

:code:

tilfældigtal1 + tilfældigtal2 + tilfældigtal3)/3);

Your dividing doubles by an integer, which is giving you integer division. So you need to divide by a double, that is still 3. 3.0 <- change to that.
Was This Post Helpful? 0
  • +
  • -

#3 Java_noob  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 21-June 09

Re: im new and need help

Posted 21 June 2009 - 11:45 AM

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

	double tilfældigtal1, tilfældigtal2, tilfældigtal3;
	tilfældigtal1 = Math.random();
	tilfældigtal2 = Math.random();
	tilfældigtal3 = Math.random();

	System.out.println( "1 tal er"+tilfældigtal1", 2 tal er"+tilfældigtal2" og 3 tal er "+tilfældigtal3" );

	double Mean, Sum;
	Mean = ((tilfældigtal1 + tilfældigtal2 + tilfældigtal3)/3.0);
	Sum = (tilfældigtal1 + tilfældigtal2 + tilfældigtal3);

	System.out.println("sum is "+Sum" og Mean is "+Mean);
	}
}


like this ? :pirate:

it still wont work :(
Was This Post Helpful? 0
  • +
  • -

#4 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: im new and need help

Posted 21 June 2009 - 11:48 AM

What isnt working? What are your errors?
Was This Post Helpful? 0
  • +
  • -

#5 Java_noob  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 21-June 09

Re: im new and need help

Posted 21 June 2009 - 11:53 AM

--------------------Configuration: Sum_and_mean_number - JDK version <Default> - <Default>--------------------
java.lang.NoClassDefFoundError: Sum_and_mean_number
Caused by: java.lang.ClassNotFoundException: Sum_and_mean_number
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
Could not find the main class: Sum_and_mean_number. Program will exit.
Exception in thread "main"
Process completed.

help me plz
Was This Post Helpful? 0
  • +
  • -

#6 mobius4139  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 118
  • Joined: 24-March 09

Re: im new and need help

Posted 21 June 2009 - 11:54 AM

Try baby stepping the program and replacing the random numbers with actual numbers for testing purposes. This will help the process of elimination for finding the actual problem.
by doing this you can determine if the problem lies with your random numbers generator or with your figuring the mean and sum.

~Bruce
Was This Post Helpful? 1
  • +
  • -

#7 Java_noob  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 21-June 09

Re: im new and need help

Posted 21 June 2009 - 12:06 PM

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

	System.out.println( "1 tal er"+2", 2 tal er"+4" og 3 tal er "+3);

	double Mean, Sum;
	Mean = ((2 + 4 + 3) / 3.0);
	Sum = (2 + 4 + 3);

System.out.println("sum is "+Sum" og Mean is "+Mean);
}
}


it still wont work when i do this.

error 1 says this

--------------------Configuration: <Default>--------------------
C:\prog\sum.java:14: ')' expected
	System.out.println( "1 tal er"+2", 2 tal er"+4" og 3 tal er "+3);
									^
C:\prog\sum.java:14: not a statement
	System.out.println( "1 tal er"+2", 2 tal er"+4" og 3 tal er "+3);
												^
C:\prog\sum.java:14: ';' expected
	System.out.println( "1 tal er"+2", 2 tal er"+4" og 3 tal er "+3);
												  ^
C:\prog\sum.java:20: ')' expected
System.out.println("sum is "+Sum" og Mean is "+Mean);
								^
C:\prog\sum.java:20: not a statement
System.out.println("sum is "+Sum" og Mean is "+Mean);
											  ^
C:\prog\sum.java:20: ';' expected
System.out.println("sum is "+Sum" og Mean is "+Mean);
												   ^
6 errors
Process completed.



and error 2 says this
--------------------Configuration: Sum_and_mean_number - JDK version  <Default> - <Default>--------------------
java.lang.NoClassDefFoundError: Sum_and_mean_number
Caused by: java.lang.ClassNotFoundException: Sum_and_mean_number
	at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
	at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
Could not find the main class: Sum_and_mean_number.  Program will exit.
Exception in thread "main" 
Process completed.


Was This Post Helpful? 0
  • +
  • -

#8 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: im new and need help

Posted 21 June 2009 - 12:30 PM

After you add the variables you need to put another '+'

"test" +Sum + " text";

Make sure the class name is the file name. Case sensitive
Was This Post Helpful? 0
  • +
  • -

#9 tanah.melayu  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 45
  • Joined: 21-February 09

Re: im new and need help

Posted 21 June 2009 - 05:06 PM

View PostFuzzyness, on 21 Jun, 2009 - 11:30 AM, said:

After you add the variables you need to put another '+'

"test" +Sum + " text";

Make sure the class name is the file name. Case sensitive


yup! he's true... i already fix your source code and this should work. Fuzzyness said it should look like this:

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

	double tilfældigtal1, tilfældigtal2, tilfældigtal3;
	tilfældigtal1 = Math.random();
	tilfældigtal2 = Math.random();
	tilfældigtal3 = Math.random();

	System.out.println( "1 tal er" + tilfældigtal1 + ", 2 tal er" + tilfældigtal2 + " og 3 tal er " + tilfældigtal3 );

	double Mean, Sum;
	Mean = ((tilfældigtal1 + tilfældigtal2 + tilfældigtal3)/3.0);
	Sum = (tilfældigtal1 + tilfældigtal2 + tilfældigtal3);

	System.out.println("sum is " + Sum + " og Mean is " + Mean);
	}
}


the biggest mistake is u miss the '+' symbol. whenever u found errors, try to understand each of them. one day, u will able to read them and know what is actually going wrong. :)

~Happy Coding~
Was This Post Helpful? 0
  • +
  • -

#10 Java_noob  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 21-June 09

Re: im new and need help

Posted 22 June 2009 - 07:29 AM

Thanks :P :) :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1