Anda di halaman 1dari 21

CSE 412/413

Lecture 2

Dr. Sifat Momen
Control Structure
if
If(true){
...........
...........
...........
}

If-else
If(true)
{
.............
.............
} else
{
.............
.............
}


Write down a simple Java program that takes an
integer as an input and determines if it is an
even number or an odd number
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package secondLectures;

/**
*
* @author Dr. Sifat Momen
* Date: 5/2/2012
*/
// the program takes an integer as an input and displays if it is even or odd
import javax.swing.*;
public class oddEven {
public static void main(String[] args){
String input = JOptionPane.showInputDialog("Please enter an integer: "); // takes the input as string
int i = Integer.parseInt(input); // converts the string to integer
if(i%2==0)
JOptionPane.showMessageDialog(null, "" + i + " is an even number");
else
JOptionPane.showMessageDialog(null, "" + i + " is an odd number");


}


}

Loops
Repeats within a block
Different kinds of loops
While loop
For loop
Do while loop
While loop
While(condition)
{
...........
...........
...........
}
Example
Hailstone sequence / Collatz conjecture
http://en.wikipedia.org/wiki/Collatz_conjecture
Start with any positive integer
If the integer is even, divide the integer by 2 else multiply
by 3 and add 1
A sequence of integers will be produced and it is
conjectured that the sequence should ultimately reach 1
E.g. 22 -> 11->34->17->52->26->13->40->20->10->5->16->8-
>4->2->1
Write down a simple Java program that takes an integer as
an input and print out the hailstone sequence until it
reaches 1

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package secondLectures;

/**
*
* @author Dr. Sifat Momen
* Date: 5/02/2012
*/
import javax.swing.*;
public class hailstoneSequence {

public static void main(String[] args){
String input = JOptionPane.showInputDialog("Please enter any posistive integer");
int n = Integer.parseInt(input);
System.out.print(n + "-->");
while(n!=1){
if(n%2==0) // i.e. if it is even
n = n/2;
else
n = (3*n)+1;

if(n!=1)
{
System.out.print(n + "-->");
}
}
System.out.println(n);
}
}




Write a simple Java Program that takes two
integers as an input and determine the GCD of
the two numbers
import javax.swing.*;
public class GCD {
public static void main(String[] args){
String str1, str2;
str1 = JOptionPane.showInputDialog("Enter the first integer: ");
str2 = JOptionPane.showInputDialog("Enter the second integer: ");

int i = Integer.parseInt(str1);
int j = Integer.parseInt(str2);

while(i!=j)
{
if(i>j)
i-=j;
else
j-=i;

}
JOptionPane.showMessageDialog(null, "GCD: " + i);
}

}
For loop
for(initialization; condition; increment/decrement)
{
.............
........
}
Write down a simple program that displays the following:
Num Square Cube
0 0 0
1 1 1
2 4 8
3 9 27
4 16 64
5 25 125
6 36 216
7 49 343
8 64 512
9 81 729
10 100 1000
public class squareCube {
public static void main(String[] args){
System.out.println("Num\tSquare\t\tCube");
for(int i = 0; i <= 10; i++)
{
System.out.println(i + "\t\t" + (i*i) + "\t\t" +
(i*i*i));
}
}

}

OOP
Every Java program is a class
To use the methods of a particular class in
another class, an instance of the class (or
object) needs to be made.
Instance of a class is made using the new
keyword
OOP(Cont...)
Write a simple java program called
simpleMath.java that has no main method but
has the following methods
min(int a, int b): the method compares two integers
and returns the one that is minimum
max(int a, int b): the method compares two integers
and returns the one that is maximum
sum(int a, int b): takes two integers, add them up and
returns their sum
diff(int a, int b): takes two integers compute their
difference and return the non-negative integer.



OOP(Cont...)
Once simpleMath.java is made, create
another Java program called
simpleMathTest.java
Within simpleMathTest.java, create an
instance of simpleMath.java which would
enable to use the methods of simpleMath.java
simpleMath myMath = new simpleMath();
In this case myMath is the instance (object) of
SimpleMath
simpleMath.java
public class simpleMath {

public int min(int a, int b){ // compares two integers and returns the
lower integer
if(a<b)
return a;
return b;
}

public int max(int a, int b){ // compares two integers and returns the
larger integer
if(a>b)
return a;
return b;
}


simpleMath.java(Cont...)
public int sum(int a, int b){
return (a+b);
}

public int diff(int a, int b){
if(a>b)
return (a-b);
return (b-a);
}

simpleMathTest.java
public class simpleMathTest {
public static void main(String[] args){
simpleMath myMath = new simpleMath();
System.out.println("Maximum: " + myMath.max(5, 4));
System.out.println("Minimum: " + myMath.min(-2, 3));
System.out.println("Sum: " + myMath.sum(7, 3));
System.out.println("Difference: " + myMath.diff(-3, -
4));
}
}

Output
Maximum: 5
Minimum: -2
Sum: 10
Difference: 1

Anda mungkin juga menyukai