Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
34 views

Object Oriented Programming

The document contains 7 sections that demonstrate different Java programming concepts: 1) If/else and multiple selection statements 2) Switch multiple selection 3) Do/while loop 4) Nested for loops to print a pattern 5) Creating and calling methods 6) Creating and accessing an array 7) Initializing an array with values and printing them

Uploaded by

MohdHarith
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

Object Oriented Programming

The document contains 7 sections that demonstrate different Java programming concepts: 1) If/else and multiple selection statements 2) Switch multiple selection 3) Do/while loop 4) Nested for loops to print a pattern 5) Creating and calling methods 6) Creating and accessing an array 7) Initializing an array with values and printing them

Uploaded by

MohdHarith
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

1) If else multiple selection

public class test1{

public static void main(String []args){

int age;

age = 15;

if ((age >=10) && (age <= 20))

System.out.println("you are qualified");

else if ((age >20) && (age <=30))

System.out.println("You are not qualified");

else

System.out.println("Input error");

2. Switch Multiple Selection

public class test1{

public static void main(String []args){

char raceCode;

raceCode = 'M';

switch (raceCode){

case 'M': System.out.println("Malay"); break;

case 'C': System.out.println("Chinese"); break;

case 'I': System.out.println("Indian"); break;

default: System.out.println("Others");

}
}

3. Belajar do while

public class belajar {

public static void main(String []args){

int i;

i=0;

do {

System.out.println(i);

i++;

while (i<5);

4. nested loops

public class test1 {

public static void main(String []args){

for ( int line =1; line <= 5; line ++)

for (int column = 1; column <= line; column++)

System.out.print("*");

System.out.println();
}

5. Method

//this code is to create method for an object

public class obj {

public static void main(String []args){

//declare and initiate value

int a=67;

int b=80;

int max = getMax (a,b);

System.out.println("Maximum is "+ max);

//this code is a method to find max number

public static int getMax (int no1, int no2)

int max;

if (no1 > no2)

max = no1;

else

max = no2;

return max;

6. Array

//this code is to create array

public class arry {


public static void main(String []args){

String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};

for (int i = 0; i < cars.length; i++) {

System.out.println(cars[i]);

7. Assignment array

Soalan 1

//this code is to create array

public class Assignment {

public static void main(String []args)

//coding untuk initiate array valu

int[] value = {1,2,3,4,5,4,4,3};

//loop untuk baca dman display array value

for (int i = 0; i < value.length; i++) {

System.out.println(value[i]);

You might also like