You now know a lot of Java: variables, operators, conditions, loops, arrays and methods. The next step is to use them to solve problems.
A problem tells you what a program must do, but not how. So where do you start? Most programmers use four steps:
Understand the problem
Plan the solution
Write the code
Test the code
In this chapter, we use the four steps to solve one of our Java Practice Problems, from start to finish. Then it is your turn to solve it in our practice editor.
The Problem
This problem is called Celsius to Fahrenheit. Celsius and Fahrenheit are two ways to measure temperature. Here is the problem:
Read a temperature in Celsius from input.
Use this formula: fahrenheit = celsius * 9/5 + 32
Print the result like this: 25 Celsius = 77.0 Fahrenheit
You do not need to know how to convert temperatures. The problem gives you the formula, and your job is to turn it into code. Many problems work like this.
Step 1: Understand the Problem
Before you write any code, find out what goes in, and what must come out:
Input: a whole number, the temperature in Celsius. For example 25.
Output: one line of text, exactly like 25 Celsius = 77.0 Fahrenheit.
Then solve the example by hand. Put 25 into the formula, and calculate from left to right: 25 * 9 is 225, 225 / 5 is 45, and 45 + 32 is 77. That matches the 77.0 in the example output.
If you can solve the problem by hand, you know what the program must do.
Step 2: Plan the Solution
Write the steps in plain words, before you write any Java code. This is called pseudocode (code written in plain words, that no computer can run):
fahrenheit is a double, so Java prints it with a decimal: 77.0. The output is exactly what the problem asks for. Are we done? Not yet!
Step 4: Test the Code
One correct answer does not mean that the program always works. Test it with more temperatures, where you know the right answer.
Choose a temperature below, and follow it through the program:
Code
Inside the program
Output
Expected
Explanation
The program passes four tests, but fails with 37. In Java, an int divided by an int gives an int, and the decimals are cut off (see Arithmetic Operators).
Why did the other tests not find the bug? Because 225, 0, 900 and -360 can all be divided by 5 with nothing left over. That is why you should test many different values.
Fix and Test Again
To fix the bug, write 9.0 instead of 9. Now Java uses decimal division, and keeps the decimals.
It is also a good idea to put the code in a method. Then you can test all the temperatures in one run:
Now solve Celsius to Fahrenheit yourself, in our practice editor. The starter code already reads the temperature from input, with a Scanner (see User Input). You write the rest.
When you press Submit, your program is checked with several temperatures. You will learn how this works in the Test Cases chapter.
Tips for Solving Problems
Read the problem twice, and find the input and the output.
Solve one example by hand, before you write any code.
Test more than the example. Also try 0, negative numbers, and values that give decimals.
If the output is wrong, print your variables to see what they contain (see Debugging).
W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning.
Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness
of all content. While using W3Schools, you agree to have read and accepted our terms of use,
cookies and
privacy policy.
You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.