Lesson 3 — Your First Java Program

A simple intro to Java

Previous :: Next

Basic Instructions

  1. Read the problem that you are supposed to solve.
  2. Try it out on your own here.
  3. Come back here and check your work. Remember, there is more than one way to solve a problem, so your code doesn’t have to look exactly like mine!

Hello World!

Problem

For this one create a simple program that prints out: Hello world!

Solution

Code:

public class MyClass {
public static void main(String args[]) {
System.out.print("Hello world!");
}
}

Output:

Hello world!

Mathematical!

Problem

Let’s do some math. Make an integer variable x and initialize it with a value, it’s up to you. Create another integer variable answer and leave this one blank for now. Then take x and multiply by 5 and subtract 32. Put our answer in the answer variable and then print out x and answer.

Solution

Code:

public class MyClass {
public static void main(String args[]) {
int x = 50;
int answer;
answer = (x / 5) - 32;
System.out.print("("+ x + "/5) - 32 = " + answer);
}
}

Output:

(50/5) - 32 = -22

Got questions? Ask them below!

Previous :: Next