473,382 Members | 1,252 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Problem with a program that is suppose to compute mas

I am new to Java and am currently taking a class where one of our assignments is to compute the mass of an aluminum block. We are currently on week 2 and only into our second chapter. I am completely lost! I have followed every example from the book I can find and I am getting errors all over the place. If anyone could help I would be extremely greatful! Thanks!

Here is what I have so far!

public class W3Ex33 {

//main(): application entry point
public static void main(String[] args) {
int density = 2.7;
int volume = length * Width * height;
int mass = density * volume;
int length
int width
int height

Scanner stdin = new Scanner(System.in); //set up input string

System.out.print("Length is: "); //read int length
double length = stdin.nextInt();

System.out.print("Width is: "); //read int width
double width = stdin.nextInt();

System.out.print("Height is: "); //read int height
doublt height = stdin.nextInt();

double volume = length * Width * height; //convert to metric equivalents

double mass = density * volume; //perform mass calculation

System.out.println("An aluminum block with:"); //display results
System.out.println(" length " + length);
System.out.println(" width " + width);
System.out.println(" height " + height);
System.out.println("has a Mass of " + mass);

} //End method main

} // end class W3Ex33



Once again, if anyone can help! Thanks so much!
Apr 13 '07 #1
14 1993
r035198x
13,262 8TB
In the code below

Expand|Select|Wrap|Line Numbers
  1.  
  2. int volume = length * Width * height;
  3. int mass = density * volume;
  4. int length
  5. int width
  6. int height
  7.  
You use length, Width and height before defining them. When you do define them, the statements are missing the ; at the end. Go through the rest of your code and check for any similar mistakes.
Apr 13 '07 #2
JosAH
11,448 Expert 8TB
@OP: did you read what the compiler was trying to tell you? Compiler diagnostics
are extremey useful if you know how to read them. From a quick scan:

1) width and Width are two separate words/identifiers;
2) no semicolons after the definition of a few variables;
3) use before set errors for a few variables;
4) scanning ints and assigning them to doubles.

First follow up the advice the compiler tried to give to you.

kind regards,

Jos
Apr 13 '07 #3
Here are all the corrections I could personally find and the results left over:
I still get 7 errors:

public class W3Ex33 {

//main(): application entry point
public static void main(String[] args) {
double length;
double width;
double height;
double density = 2.7;
double volume = length * width * height;
double mass = density * volume;


Scanner stdin = new Scanner(System.in); //set up input string

System.out.print("Length is: "); //read int length
double length = stdin.nextInt();

System.out.print("Width is: "); //read int width
double width = stdin.nextInt();

System.out.print("Height is: "); //read int height
double height = stdin.nextInt();

double volume = length * width * height; //convert to metric equivalents

double mass = density * volume; //perform mass calculation

System.out.println("An aluminum block with:"); //display results
System.out.println(" length " + length);
System.out.println(" width " + width);
System.out.println(" height " + height);
System.out.println("has a Mass of " + mass);

} //End method main

} // end class W3Ex33





The Errors I get are as follows:

From the Command Prompt Window:

C:\W3Ex33>javac W3Ex33.java
W3Ex33.java:31: cannot find symbol
symbol : class Scanner ( I get this error message twice for line to set up input strings.

then for each input I am getting a length width height volume and mass are already defined in main(java.lang.String[])

Thanks For the HELP!

ALI
Apr 13 '07 #4
Ganon11
3,652 Expert 2GB
When you are reading into the variables, you say something like

Expand|Select|Wrap|Line Numbers
  1. double length = stdin.nextInt();
which is trying to declare a new variable called length. You should say

Expand|Select|Wrap|Line Numbers
  1. length = stdin.nextInt();
to assign a value to the alread-assigned variabled named length. You do the same thing when initializing volume and mass.

Alternatively, you can get rid of the variable declarations at the beginning of main() except for density.
Apr 13 '07 #5
public class W3Ex33 {

//main(): application entry point
public static void main(String[] args) {
double length;
double width;
double height;
double density = 2.7;
double volume = length * width * height;
double mass = density * volume;


Scanner stdin = new Scanner(System.in); //set up input string

System.out.print("Length is: "); //read int length
length = stdin.nextInt();

System.out.print("Width is: "); //read int width
width = stdin.nextInt();

System.out.print("Height is: "); //read int height
height = stdin.nextInt();

volume = length * width * height; //convert to metric equivalents

mass = density * volume; //perform mass calculation

System.out.println("An aluminum block with:"); //display results
System.out.println(" length " + length);
System.out.println(" width " + width);
System.out.println(" height " + height);
System.out.println("has a Mass of " + mass);

} //End method main

} // end class W3Ex33





The Errors I get are as follows: 2 errors left!

From the Command Prompt Window:

C:\W3Ex33>javac W3Ex33.java
W3Ex33.java:31: cannot find symbol
symbol : class Scanner ( I get this error message twice for line to set up input strings.
They are in reference to the word Scanner twice on that line!

Thanks again! ALI
Apr 13 '07 #6
JosAH
11,448 Expert 8TB
W3Ex33.java:31: cannot find symbol
symbol : class Scanner ( I get this error message twice for line to set up input strings.
They are in reference to the word Scanner twice on that line!

Thanks again! ALI
The Scanner class is defined in the "java.util" package so you should import it.
Only the classes defined in "java.lang" don't need an explicit import statement.

At the top of your source file you should add a line:
Expand|Select|Wrap|Line Numbers
  1. import java.util.Scanner;
... see what happens then.

kind regards,

Jos
Apr 13 '07 #7
I guess when they mean trial and error, atleast in my case they definately mean the error part. now I am getting a completely new message:

Here is my code:

import java.util.Scanner;
public class W3Ex33 {

//main(): application entry point
public static void main(String[] args) {
double length;
double width;
double height;
double density = 2.7;
double volume = length * width * height;
double mass = density * volume;


Scanner stdin = new Scanner(System.in); //set up input string

System.out.print("Length is: "); //read int length
length = stdin.nextInt();

System.out.print("Width is: "); //read int width
width = stdin.nextInt();

System.out.print("Height is: "); //read int height
height = stdin.nextInt();

volume = length * width * height; //convert to metric equivalents

mass = density * volume; //perform mass calculation

System.out.println("An aluminum block with:"); //display results
System.out.println(" length " + length);
System.out.println(" width " + width);
System.out.println(" height " + height);
System.out.println("has a Mass of " + mass);

} //End method main

} // end class W3Ex33



Here is the error message I am getting:

I get it in three places on the same line:

W3Ex33.java:27: variable length might not have been initialized;
double volume = length * width * height;

W3Ex33.java:27: variable width might not have been initialized;
double volume = length * width * height;

W3Ex33.java:27: variable height might not have been initialized;
double volume = length * width * height;

Thanks Again! ALI
Apr 13 '07 #8
JosAH
11,448 Expert 8TB
IHere is the error message I am getting:

I get it in three places on the same line:

W3Ex33.java:27: variable length might not have been initialized;
double volume = length * width * height;

W3Ex33.java:27: variable width might not have been initialized;
double volume = length * width * height;

W3Ex33.java:27: variable height might not have been initialized;
double volume = length * width * height;

Thanks Again! ALI
Yup, that's what you get if you're trying to use variables before they've got a
sensible value. Don't try to multiply those three variables until after they've
received a sensible value, i.e. after you've scanned the values for them

kind regards,

Jos
Apr 13 '07 #9
Yup, that's what you get if you're trying to use variables before they've got a
sensible value. Don't try to multiply those three variables until after they've
received a sensible value, i.e. after you've scanned the values for them

kind regards,

Jos

Well what you just wrote was like korean to me! However, i think I have figured it out, you are a genius and a saint, and I thank you from the bottom of my frazzled heart. I just hope I can get things alittle easier as the semester progresses and once again! Thanks A Million!

ALI :P
Apr 13 '07 #10
JosAH
11,448 Expert 8TB
Well what you just wrote was like korean to me! However, i think I have figured it out, you are a genius and a saint, and I thank you from the bottom of my frazzled heart. I just hope I can get things alittle easier as the semester progresses and once again! Thanks A Million!

ALI :P
It's not as complicated as it seems; this was your code:
Expand|Select|Wrap|Line Numbers
  1. double length;
  2. double width;
  3. double height;
  4. double volume= length*width*heigth;
Local variables don't have a value before you explicitly give them one, either
by initialization or by assignment. The top three local variables don't have a
value. Then all of a sudden you want to initialize variable 'volume' with the
value of the product of the other three variables. What's their value then?

The compiler is smart enough to notice that and starts to complain about it.
You don't know their values either because the user hasn't supplied values
for them yet. That only happens later in your program when you use your
Scanner object. Only then, after all three variables have a value assigned
you can calculate your formula length*width*height. Programming is not
about voodoo and psychic events you know, computers are as stupid as they
can be.

kind regards,

Jos
Apr 13 '07 #11
Ganon11
3,652 Expert 2GB
...computers are as stupid as they can be.
In fact, they are so stupid, they can only do what we tell them to do.
Apr 13 '07 #12
r035198x
13,262 8TB
... you are a genius and a saint...
Jos? Genius and saint?

Wait 'till he puts his nitpicking armour on.
Apr 14 '07 #13
JosAH
11,448 Expert 8TB
Jos? Genius and saint?

Wait 'till he puts his nitpicking armour on.
Small correction: my shiny nitpicking armour ;-)

kind regards,

Jos
Apr 14 '07 #14
r035198x
13,262 8TB
Small correction: my shiny nitpicking armour ;-)

kind regards,

Jos
Exactly what I was talking about.
Apr 14 '07 #15

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Martin Jensen | last post by:
Hi I have a problem with Qt. My class definition is this: class Button : public QObject, public Tk_Object { Q_OBJECT public: Button() {} Button(Tk_Object &p); ~Button();
13
by: ashu | last post by:
hi to all, Please read the following ques. first. assume that a bank maintains 2 kinds of accounts for customers one called as savings accounts & other as current account. the savings account...
8
by: Elliot Temple | last post by:
Problem: Randomly generate 10 integers from 0-100 inclusive, and sum them. Do that twice. What is the probability the two sums are 390 apart? I have code to do part of it (below), and I know how...
52
by: celerysoup16 | last post by:
I've written this coin toss program, and can't figure out why it isn't giving accurate results... cheers, Ben #include <stdlib.h> #include <stdio.h> #define H 1 #define T 0 #define SENTINEL...
1
by: confusedprogrammer | last post by:
In the program below i need to get it, so that it will keep going through the program asks all the question and displays the results and goes back to the beginning if the user didn't select. After...
2
by: AZRebelCowgirl73 | last post by:
Crazy as this sounds this program is actually working! I know I know it is amazing I got one to run. The only problem I am having is I want it to say for output line (1,2 3 etc.) has a length...
2
by: Tony | last post by:
Hello, As a part of a personal project, I am attempting to write a computer program that can solve the following problem: Suppose I start with a finite piece of rectangle. Within this...
4
by: geojameson | last post by:
Hello, I am working on an assignment for my 'Operating Systems' class. Our first assignment deals with creating processes using the fork(). I have most of the program already written (I can PM...
5
by: luke | last post by:
Hi, my program has a memory leak and I can't handle it. Basically, there are two classes, class_a and class_b, and one object of each class: class_a obj_a, and class_b obj_b. One of the methods...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.