Help with Java program calculation

Click For Summary
SUMMARY

The forum discussion centers on a Java program designed to convert 22 square meters into square kilometers. The user initially encounters an issue where the output is zero due to integer division. The solution provided suggests declaring the variables 'sqmetre' and 'kilometrerate' as double types to ensure accurate calculations. The final output correctly reflects the conversion by dividing square meters by 1 million.

PREREQUISITES
  • Understanding of Java programming syntax and structure
  • Knowledge of data types in Java, specifically int and double
  • Familiarity with basic arithmetic operations in programming
  • Concept of unit conversion in mathematics
NEXT STEPS
  • Learn about Java data types and their implications on arithmetic operations
  • Explore the concept of type casting in Java to avoid integer division issues
  • Study Java's System.out.println() method for formatted output
  • Investigate best practices for variable initialization and code optimization in Java
USEFUL FOR

Java developers, programming students, and anyone interested in understanding unit conversion and arithmetic operations in Java.

Darkstar3000
Messages
28
Reaction score
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
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.
 
The main problems are that your program
1) does no calculations
2) displays an uninitialized variable, sqmtosqk
 

Similar threads

  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
7K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K