0% found this document useful (0 votes)
7 views8 pages

PRACTICAL QUESTIONS Android Studio

Uploaded by

Fazilah K
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views8 pages

PRACTICAL QUESTIONS Android Studio

Uploaded by

Fazilah K
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

EXECUTIVE DIPLOMA

IN INFORMATION TECHNOLOGY

PRACTICAL 2 (WEEK – 2)

COURSE CODE : UFH2011


COURSE TITLE : Mobile Application Development
STUDENT NAME : SITI FAZILAH BINTI KASIM
MATRIC NUMBER : UFH220033
LECTURER : Prof. Dr. Ong Sim Ying
PRACTICAL QUESTIONS

1. Write the following operation in the main method to print their output:
-3 + 5 * 8
(105+7) % 4
34 + -5 * 8 / 6
7 + 15 / 2 * 6 - 7 % 3

ANSWER#1:
public class practicalNum1 {
public static void main(String args[]){
System.out.println("-3 + 5 * 8");
System.out.println("(105+7) % 4 ");
System.out.println("34 + -5 * 8 / 6 ");
System.out.println("7 + 15 / 2 * 6 - 7 % 3");
}
}

Result:
2. Write Java codes to produce the following output:

ANSWER#2:

public class PracticalNum2 {


public static void main(String args[]){
int NumA = 7;
int NumB = 9;
int Result = (NumA * NumB);
System.out.println("RESULT");
System.out.println("First Integer: " + NumA);
System.out.println("Second Integer: " + NumB);
System.out.println("Multiplication Output: " + Result);
}
}

Result:
3. By using for loop, write the codes to produce the following output:

public class PracticalNum3 {


public static void main(String args[]){

int VarX = 3;
int Result = 1;

System.out.println("Input Integer: " + VarX);


for(int i = 1; i<=10; i++){
System.out.print(VarX + " x " + i + " = ");
Result = i * VarX;
System.out.println(Result);
}
}
}

Result:
4. Modify the codes in Q3 to get the sum of all the output (3, 6, …, 30).

public class PracticalNum4 {


public static void main(String args[]){

int VarX = 3;
int Result = 1; //Use to store the result, try to change to 0 and
see the result change
int sum = 0;
System.out.println("Input Integer: " + VarX);
for(int i = 1; i<=10;i++){
System.out.print(VarX + " x " + i + " = ");
Result = i * VarX;
sum += Result;
System.out.println(Result);
}
System.out.println("The total sum of all results: " + sum);
}
}

result;
5. By using switch statement, display the colors according to the RGB input.

Sample output:

Color RED GREEN BLUE Binary Decimal


Blue 0 0 1 00000001 1
Green 0 1 0 00000010 2
Cyan 0 1 1 00000011 3
Red 1 0 0 00000100 4
Magenta 1 0 1 00000101 5
Yellow 1 1 0 00000110 6
White 1 1 1 00000111 7

public class PracticalNum5 {


public static void main(String args[]) {
boolean red = true;
boolean green = false;
boolean blue = true;

String colorOutput = getcoloroutput(red,


green, blue);
System.out.println("R: " + red + " B: " +
blue + " G: " + green);
System.out.println("Color Output: " +
colorOutput);
}

public static String getcoloroutput(boolean red,


boolean green, boolean blue) {
String color;

switch ((red ? 1 : 0) * 4 + (green ? 1 : 0) *


2 + (blue ? 1 : 0) * 1) {
case 1:
color = "Blue";
break;
case 2:
color = "Green";
break;
case 3:
color = "Cyan";
break;
case 4:
color = "Red";
break;
case 5:
color = "Magenta";
break;
case 6:
color = "Yellow";
break;
case 7:
color = "White";
break;
default:
color = "Unknown";
break;
}
return color;
}
}

Result:
6. Create a Java method to accept student’s mark (0 – 100 marks) and return grade to the main
method. Main method will display the grade that the student obtained.

public class PracticalNum6 {


// Method to calculate grade based on the mark
public static String studentGrade(int mark) {
if (mark >= 90 && mark <= 100) {
return "A";
} else if (mark >= 80 && mark < 90) {
return "B";
} else if (mark >= 70 && mark < 80) {
return "C";
} else if (mark >= 60 && mark < 70) {
return "D";
} else if (mark >= 0 && mark < 60) {
return "F";
} else {
return "Invalid mark";
}
}

public static void main(String args[]) {


int studentMark = 55; // Example of student mark
String grade = studentGrade(studentMark); // Get the
grade A,B,C,D,F based on the mark
System.out.println("The mark entered is: " + studentMark
+ "%");
System.out.println("The grade for the student is: " +
grade);
}
}

Result:

You might also like