Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Let’s do Java

CS F213: Object Oriented Programming

Dipanjan Chakraborty
Department of CS&IS

1/6
Program 1

Write a “Hello World” program in Java

Points to note:
– Java is whitespace agnostic meaning java doesnt care about indentation

– There must be at least one class java strcit OOP

– Execution starts at the main() method


– Convention/Requirement: the class name is the same as the filename
– The main() method is static. What does it mean?
● We will have a bigger discussion about the static keyword later

Now modify the program to have two main functions in two separate classes. How
would you run this program?

2/6
Program 2
● Write a class NumberTester with one integer data
member number and a method testOddOrEven().
Using this test if different numbers are odd or even
from the main() method.

Points to note:
– How do the if and else constructs work in Java?
– What are valid identifiers in Java?

3/6
Program 3

Modify Program 2 to add a new method
isPrime() to the NumberTester class

Points to note:
– How does the for loop work in Java?

4/6
Program 4

Convert Program 3 to use:
– The while loop construct
– The do...while loop construct

What is the working difference between the
while and do...while loop?

5/6
Other constructs

In the lab you have also worked on / will work on:
– switch and case
– for-each

You have been introduced to arrays

These are similar to how they work on C, other than some
minor syntactical differences. We will get to them
eventually in the lectures

6/6

You might also like