0% found this document useful (0 votes)
454 views5 pages

2.2 Module 2 Basic Java Programming Language

The document provides examples of Java programming concepts including: 1) A pay computation program that demonstrates defining and calling methods. 2) A basic math program that shows arithmetic operators on integers and doubles. 3) An increment operator program that uses pre- and post-increment. 4) Two switch-case programs with different outputs to demonstrate fall-through behavior. 5) A Holder class example that passes an object and increments a field. 6) A suggested change from a switch-case to if-else statements for a given example. 7) A question about why a switch-case example using a String instead of int causes a compile error.

Uploaded by

azphyo
Copyright
© Attribution Non-Commercial (BY-NC)
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)
454 views5 pages

2.2 Module 2 Basic Java Programming Language

The document provides examples of Java programming concepts including: 1) A pay computation program that demonstrates defining and calling methods. 2) A basic math program that shows arithmetic operators on integers and doubles. 3) An increment operator program that uses pre- and post-increment. 4) Two switch-case programs with different outputs to demonstrate fall-through behavior. 5) A Holder class example that passes an object and increments a field. 6) A suggested change from a switch-case to if-else statements for a given example. 7) A question about why a switch-case example using a String instead of int causes a compile error.

Uploaded by

azphyo
Copyright
© Attribution Non-Commercial (BY-NC)
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/ 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

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy