You are on page 1of 10

MTS3033 A201

MTS 3033 Object Oriented Programming


Assignment #3 –Date 21 October 2020
Mohamad Faiz Bin Abu Bakar D20191088851
Write a Java Program according to the following algorithm and run the program
using command prompt (cmd) and Netbeans. Screenshot both output.
i. Algorithm 1 (5 marks)
STEP 1: START
STEP 2: DEFINE a, b
STEP 3: SET line=5
STEP 4: SET a=1. REPEAT STEP 5 to 10 UNTIL a<=line
STEP 5: SET c=line
STEP 6: REPEAT STEP 7 and 8 until c> =1
STEP 7: IF c != a then PRINT c
Else PRINT ^
STEP 8: c=c-1
STEP 9: PRINT new line
STEP 10: a=a+1
STEP 11: END

Code :
package javaapplication21;
public class JavaApplication21 {

public static void main(String[] args) {


int a,b;
int line=5;

a=1;
while(a<=line){
int c=line;
while(c>=1){
if(c!=a)
System.out.print(c);
else
MTS3033 A201

System.out.print("^");
c=c-1;
}
System.out.print("\n");
a=a+1;
}
}
}

Output:
Neatbeans :

Cmd :
MTS3033 A201

ii. Algorithm 2 (5 marks)

STEP 1: START
STEP 2: SET size = 4
STEP 3: SET r = size. REPEAT STEP 4 to 9 until r !=0
STEP 4: SET s=0. REPEAT STEP 5 UNTIL s<(size – r)
STEP 5: PRINT ** and SET s=s+1
STEP 6: SET t=0. REPEAT STEP 7 UNTIL t< (2*r – 1)
STEP 7: PRINT * and SET t=t+1
STEP 8: PRINT new line
STEP 9: SET r=r-1
STEP 10: END

Code :

package javaapplication22;

public class JavaApplication22 {

public static void main(String[] args) {


int size=4;
int r=size;

while(r!=0){
int s=0;
while(s<(size-r)){
System.out.print("**");
s=s+1;
}
int t=0;
while(t<(2*r-1)){
System.out.print("*");
t=t+1;
}
MTS3033 A201

System.out.print("\n");
r=r-1;
}
}
}

Output :

Neatbeans :

Cmd:
MTS3033 A201

iii. Algorithm 3 (5 marks)

STEP 1: START
STEP 2: DEFINE i, j
STEP 3: SET n=7
STEP 4: PRINT ‘Right Angle Triangle’
STEP 5: SET i=1. REPEAT STEP 6 to 8 UNTIL i
STEP 6: SET j=1. REPEAT STEP 7 UNTIL j<=i
STEP 7: PRINT * and SET j=j+1
STEP 8: PRINT new line and SET i=i+1
STEP 9: END

Code :
package javaapplication23;

public class JavaApplication23 {

public static void main(String[] args) {


int i,j;
int n=7;
System.out.println("Right Angle Triangle");
i=1;
while(i<n){
j=1;
while(j<=i){
System.out.print("*");
j=j+1;
}
System.out.print("\n");
i=i+1;
}
}
}
MTS3033 A201

Output :

Neatbeans :

Cmd:
MTS3033 A201

iv. Algorithm 4 (5 marks)

STEP 1: START
STEP 2: SET m=1
STEP 3: SET k=0. REPEAT STEP 4 TO STEP 8 UNTIL k<6
STEP 4: SET L=1. REPEAT STEP 5 and STEP 6 UNTIL L
STEP 5: PRINT m
STEP 6: SET m=m+1, L=L+1
STEP 7: PRINT new line
STEP 8: SET k= k+1
STEP 9: END

Code :

package javaapplication24;

public class JavaApplication24 {

public static void main(String[] args) {


int m=1;
int k=0;
while(k<6){
int L=1;
while(L==1){
System.out.print(m);
m=m+1;
L=L+1;
}
System.out.print("\n");
k=k+1;
}
}
MTS3033 A201

Outpot :

Neatbeans:

Cmd :
MTS3033 A201

Code (change position of public static void main)

public class computer {


computer(){
System.out.println("Constructor of Computer class");

}
public static void main(String[] args){

computer my=new computer();


laptop your=new laptop();
my.computer_method();
your.laptop_method();
}
void computer_method(){
System.out.println("Power gone! Shut down your PC
soon...");

class laptop{
laptop(){
System.out.println("Constructor of Laptop class");

}
void laptop_method(){
System.out.println("99% Battery available");
}
}
MTS3033 A201

Outpot :

When switch the main to bottom part of the code. I got error
massage. I think it because compiler always search main
method first.

You might also like