Help with Java program calculation

In summary, the given conversation involves writing a program to convert 22 square metres into square kilometres. The program should divide the value of square metres by 1 million to produce square kilometres. It is noted that the answer is not zero, so the output should have a value other than 0. To ensure this, the variables sqmetre and kilometrerate should be declared as double rather than long or int. The program should perform calculations and display the result without any unnecessary statements.
  • #1
Darkstar3000
29
0
Question: Write a program to convert 22 square metres into square kilometres.
Note: divide square metres by 1 million to produce square kilometres.
Note also that the answer is not zero!

I know the note says the answer is not zero but shouldn't it have some value other than 0 ?

My code
Code:
public class areaConverter{

public static void main(String []args){

    long kilometrerate = 1000000;
    
    int sqmetre = 22;    
       
    double sqmtosqk;
    
    sqmtosqk = sqmetre/kilometrerate;
    
    System.out.println("The number of square Kilometres in "+sqmetre+" square meters is " + sqmtosqk);
}
}
 
Physics news on Phys.org
  • #2
Darkstar3000 said:
Question: Write a program to convert 22 square metres into square kilometres.
Note: divide square metres by 1 million to produce square kilometres.
Note also that the answer is not zero!

I know the note says the answer is not zero but shouldn't it have some value other than 0 ?

My code
Code:
public class areaConverter{

public static void main(String []args){

    long kilometrerate = 1000000;
    
    int sqmetre = 22;    
       
    double sqmtosqk;
    
    sqmtosqk = sqmetre/kilometrerate;
    
    System.out.println("The number of square Kilometres in "+sqmetre+" square meters is " + sqmtosqk);
}
}


Hint : Declare sqmetre or kilometrerate double rather than long or int.

You have written too many statements that were not even required.
 
  • #3
The main problems are that your program
1) does no calculations
2) displays an uninitialized variable, sqmtosqk
 

Related to Help with Java program calculation

1. What is Java?

Java is a popular programming language used for creating a wide range of applications, from web and mobile applications to desktop software. It is known for its flexibility, scalability, and platform independence.

2. How does Java help with program calculations?

Java has built-in mathematical functions and operators that allow for efficient and accurate calculations. Additionally, it offers various libraries and APIs for more complex calculations and data manipulation.

3. What are the basic syntax rules for writing calculations in Java?

The basic syntax for calculations in Java follows the standard mathematical conventions, such as using parentheses for grouping and following the order of operations. Additionally, Java uses specific operators for different types of calculations, such as + for addition, - for subtraction, * for multiplication, and / for division.

4. How do I input values for calculations in a Java program?

Java offers various methods for inputting values into a program, such as using the Scanner class or command-line arguments. The specific method used will depend on the specific needs of the program and the user's input preferences.

5. Are there any common errors to watch out for when writing calculations in Java?

Some common errors to watch out for when writing calculations in Java include using the wrong data type, forgetting to initialize variables, and using incorrect syntax for mathematical operations. It is important to thoroughly test and debug your code to catch any potential errors.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Programming and Computer Science
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
18
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
6K
Back
Top