You are on page 1of 8

A2

1. package javaapplication5;
import javax.swing.*;
public class JavaApplication5 {
public static void main(String[] args) {
int i=1;
while (i<=3) {
method1 (i,2);
i++;
}
}
public static void method1 (
int i, int num) {
for (int j=1; j<=i; j++) {
System.out.print (num + "");
num*=2;
System.out.println ();
}
}
}
Answers
2
2
4
2
4
8

A2
2.)
Fahrenheit to Celsius

import java.util.*;
public class Temperature {
public static void main(String[] args) {
float temperatue;
Scanner in = new Scanner(System.in);
System.out.println("Enter temperatue in Fahrenheit");
temperatue = in.nextInt();
temperatue = ((temperatue - 32)*5)/9;
System.out.println("Temperatue in Celsius = " + temperatue);
}
}
Celsius to Fahrenheit
import java.util.*;
public class Temperature {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double celsius, fahrenheit;
System.out.print("Enter a temperature in Celsius: ");
celsius = sc.nextDouble();
fahrenheit = 32 + (celsius * 9 / 5);
System.out.println(celsius +" C = " + fahrenheit + " F");
}
}

A3
1.)
Analyze the following Java code. Simplify the code by removing unnecessary
statements.
Switch ( carModel ) {
case DELUXE :
addAirConditioning() ;
addRadio () ;
addWheels() ;
addEngine() ;
break ;
case STANDARD :
addRadio () ;
addWheels() ;
addEngine() ;
break ;
default :
addWheels() ;
addEngine() ;
break ;
}
Answer
Switch(carModel)
{
Case DELUXE :
addAirConditioning();
caseSTANDARD:
addRadio();
default:
addWheels();
addEngine();
}

A3
2.)
Analyze the following Java for loop and convert it to while loop
for(int i=1; 1<11; i++)
{
System.out.println(i);
}
Answer:
Int I = 1;
while (i<11)
{
System.out.prinln(i);
}
Int i=1
While (1<11)

For Loop:
for(xx;yy;zz)
{
aa
}
While Loop:
xx
while(yy)
{
aa
zz
}
Do While:
xx
do
{
aa
zz
}
while(yy)

B1
Enter name blab la
Answer
import java.util.Scanner;
public class Navtest {
public static void main(String[] args) {
Scanner s=new Scanner (System.in);
System.out.print("Your Name?");
String name = s.nextLine();
System.out.print("Last 4 Digits of Your IC Number?");
int ic = s.nextInt();
System.out.print("The Year You Born?");
int age = 2014 - s.nextInt();
String gender = "female" ;
if (ic%2==0);
else
gender="male";
System.out.println(name + ",You are "+age+" years old "+gender+"! :" );
}
}

B2
Sum
Answer
import java.util.Scanner;
public class Sum {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n;
System.out.print("Enter a positive integer: ");
n = in.nextInt();
if (n <= 0)
System.out.println("Integer you've entered is nonpositive.");
else {
int sum = 0;
while (n != 0) {
sum += n % 10;
n /= 10;
}
System.out.println("Sum of digits: " + sum);
}
}
}

Square Root
import java.text.DecimalFormat;
import javax.swing.JOptionPane;
public class NavTest{
public static void main(String[] args) {
DecimalFormat f=new DecimalFormat("#0.00");
double y,z;
while(true) {
String input = JOptionPane.showInputDialog("Enter a positive volume to find the
square root of the value" +
" \n(negative value will end the program)\n User Input: ") ;
y = Double.parseDouble(input);
if (y<0) {
JOptionPane.showMessageDialog(null, " Input must be a larger or equal to zero!
/n Program will be ended!");
break;
z=Math.sqrt(y);
JOptionPane.showMessageDialog(null, "Program Output: Square root of "+y+" is
"+f.format(z) + "\n");
}
}
}
}

You might also like