You are on page 1of 5

Fundamentals of Java Programming

MFundamentals
odule 2 # BasoficJava
JavProgramming
a Programming Language

Lab: Fundamental Language.


1. Type in the pay computation example as shown below and verify that it works properly.
package test.ex1;
public class Compute {
public static void main(String[] args) {
float hours;
float payrate;
hours = 40f;
payrate = 12.50f;
System.out.println("The pay is: " + ComputePay(hours,
payrate));
}
static float ComputePay(float hours, float rate) {

float pay;
pay = hours * rate;
return pay;
}
}

Copy Right , BrainStream Co., Ltd., 2004 Page 1 of 1


Fundamentals of Java Programming
MFundamentals
odule 2 # BasoficJava
JavProgramming
a Programming Language

2. This program demonstrates the arithmetic operators.


package test.ex2;
class BasicMath {
public static void main(String[] args) {
// arithmetic using integers
System.out.println("Integer Arithmetic");
int a = 1 + 1;
int b = a * 3;
int c = b / 4;
int d = c - a;
int e = -d;
System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("c = " + c);
System.out.println("d = " + d);
System.out.println("e = " + e);
// arithmetic using doubles
System.out.println("\\nFloating Point Arithmetic");
double da = 1 + 1;
double db = da * 3;
double dc = db / 4;
double dd = dc - a;
double de = -dd;
System.out.println("da = " + da);
System.out.println("db = " + db);
System.out.println("dc = " + dc);
System.out.println("dd = " + dd);
System.out.println("de = " + de);
}
}

3. This program demonstrates the increment operator.


public class IncTest {
public static void main(String args[]) {
int a = 1;
int b = 2;
int c;
int d;
c = ++b;
d = a++;
c++;
System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("c = " + c);
System.out.println("d = " + d);
}
}

Copy Right , BrainStream Co., Ltd., 2004 Page 2 of 2


Fundamentals of Java Programming
MFundamentals
odule 2 # BasoficJava
JavProgramming
a Programming Language

4. Find different result between 2 programs


public class SwitchTest1 {
public static void main(String args[]) {
for (int i = 0; i < 6; i++)
switch (i) {
case 0 :
System.out.println("i is zero.");
break;
case 1 :
System.out.println("i is one.");
break;
case 2 :
System.out.println("i is two.");
break;
case 3 :
System.out.println("i is three.");
break;
default :
System.out.println("i is greater than
3.");
}
}
}

public class SwitchTest2 {


public static void main(String args[]) {
for (int i = 0; i < 6; i++)
switch (i) {
case 0 :
System.out.println("i is zero.");
case 1 :
System.out.println("i is one.");
case 2 :
System.out.println("i is two.");
case 3 :
System.out.println("i is three.");
default :
System.out.println("i is greater than
3.");
}
}
}

Copy Right , BrainStream Co., Ltd., 2004 Page 3 of 3


Fundamentals of Java Programming
MFundamentals
odule 2 # BasoficJava
JavProgramming
a Programming Language

5. Find the Result


class Test {
public static void main(String args[]) {
Holder h = new Holder();
h.held = 100;
h.bump(h);
System.out.println(h.held);
}
}
class Holder {
public int held;
public void bump(Holder theHolder) {
theHolder.held++;
}
}

6. Change from switch to if case


ackage javafun.remix;

public class SwitchTest {

public static void main(String[] args) {


int i = 0;
switch(i){
case 0 : System.out.println("0"); break;
case 1 : System.out.println("1"); break;
case 2 : System.out.println("2"); break;
default : System.out.println("Not 0,1,2"); break;
}
}
}

Hint:
If < condition >{
}else if <condition>{
}
if<condition>{
}

Copy Right , BrainStream Co., Ltd., 2004 Page 4 of 4


Fundamentals of Java Programming
MFundamentals
odule 2 # BasoficJava
JavProgramming
a Programming Language

7. Why compile Error?


package javafun.remix;

public class Switch2 {

public static void main(String[] args) {


String i = "0";
switch(i){
case "0" : System.out.println("0"); break;
case "1" : System.out.println("1"); break;
case "2" : System.out.println("2"); break;
default : System.out.println("Not 0,1,2"); break;
}
}
}

Copy Right , BrainStream Co., Ltd., 2004 Page 5 of 5

You might also like