0% found this document useful (0 votes)
26 views13 pages

FINALS Lab Activity 4-7

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

FINALS Lab Activity 4-7

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

Activity 1.0.

Instruction: In this activity, students will distinguished the different data types. How
to initialize and practice the proper used of every variable and its value. The code
fragment is already given below. Fill-in the missing words to complete the execution
of the program.

_______ studentName = your full name;


_______ studentAge = write your age here;
_______ studentFee = 75.25f;
_______ middle_Initial = place here your middle name ;

// Print variables
System.out.println("Student name: " + studentName);
System.out.println("Student age: " + studentAge);
System.out.println("Student fee: " + studentFee);
System.out.println("Middle Initial: " + _______);

1. What is the rule of plus (+) sign in the print variables?


__________________________________________________________________________________
2. Change the value of your studentName to a number 100. What happened?
________________________________________________________________________

Activity 2.0.
Instruction: Write a program to print half of the number as entered by the user.
Given: number = 500;
1. Use int to declare the variable number. Record the output. If error occur, write
here the error message.
___________________________________________________________________________________.
2. Change the data type use to “double”. Record the
output_________________________.
3. 500 is an integer, change your datatype to string. If JVM show you an error
message, identify what kind of error is it and write it here. _________________________.
Activity 3.0.
Instruction: Compile and run the given program below.
Source Code:
int time = 22;
if (time < 10) {
System.out.println("Good morning.");
} else if (time < 18) {
System.out.println("Good day.");
} else {
System.out.println("Good evening.");
}

1. What is the output of the given source code?_____________________________________


2. Modify the given source code above. Change the given condition (time < 18) to
( time > 20). Record the output.
_____________________________________________________
3. Explain how the else - if statement work.
_____________________________________________________________________________________
_____________________________________________________________________________________

Activity 4.0.
Instruction: In this activity you’re going to learn on how switch statement works.
Copy the source code below.
Source code:

int day = 3;
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
case 6:
System.out.println("Saturday");
break;
case 7:
System.out.println("Sunday");
break;
}
1. Modify the source code above. Add the code fragment below to the last part of
your case. Change the value of day to 9. Record the output.
______________________________

case 7:
System.out.println("Today is Sunday");
break;
default:
System.out.println("Looking forward to the Weekend");
}
2. Explain how switch statement works.
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
Activity 5.0. Type the given source code and identify the output

import java.util.Scanner;

public class ifToSwitchConversion {

public static void main(String [] args) {

// Declare a Scanner and a choice variable


Scanner stdin = new Scanner(System.in);
int choice = 0;

System.out.println("Please enter your choice (1-4): ");


choice = stdin_______;

if(_________)
{
System.out.println("You selected 1.");
}
else if(choice == 2 || choice == 3)
{
System.out.println("You selected 2 or 3.");
}
_____ if(choice == 4)
{
System.out.println("You selected 4.");
}
else
{
System.out.println("Please enter a choice between 1-4.");
}

ACTIVITY 6.0. Run this source code and record the output.

import java.util.Scanner;
public class LaboratoryActivity {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int choice;

// Displaying the menu options


System.out.println("=== Laboratory Activity Menu ===");
System.out.println("1. Calculate Area of a Circle");
System.out.println("2. Convert Celsius to Fahrenheit");
System.out.println("3. Find the Largest of Three Numbers");
System.out.println("4. Check if a Number is Even or Odd");
System.out.println("5. Exit");
System.out.print("Enter your choice (1-5): ");

// Getting the user's choice


choice = scanner.nextInt();

// Switch statement to handle the user's choice


switch (choice) {
case 1:
// Calculate Area of a Circle
System.out.print("Enter the radius of the circle: ");
double radius = scanner.nextDouble();
double area = Math.PI * radius * radius;
System.out.println("The area of the circle is: " + area);
break;

case 2:
// Convert Celsius to Fahrenheit
System.out.print("Enter temperature in Celsius: ");
double celsius = scanner.nextDouble();
double fahrenheit = (celsius * 9/5) + 32;
System.out.println("Temperature in Fahrenheit: " + fahrenheit);
break;

case 3:
// Find the Largest of Three Numbers
System.out.print("Enter the first number: ");
int num1 = scanner.nextInt();
System.out.print("Enter the second number: ");
int num2 = scanner.nextInt();
System.out.print("Enter the third number: ");
int num3 = scanner.nextInt();
int largest = Math.max(num1, Math.max(num2, num3));
System.out.println("The largest number is: " + largest);
break;

case 4:
// Check if a Number is Even or Odd
System.out.print("Enter a number: ");
int number = scanner.nextInt();
if (number % 2 == 0) {
System.out.println(number + " is Even.");
} else {
System.out.println(number + " is Odd.");
}
break;

case 5:
// Exit
System.out.println("Exiting the program. Goodbye!");
break;
default:
// If the user enters an invalid option
System.out.println("Invalid choice. Please enter a number between 1 and
5.");
break;
}

scanner.close();
}
}

ACTIVITY 7.0. Sample array problem.


class Main {
public static void main(String[] args) {

// create an array
int[] age = {12, 4, 5, 2, 5};

// access each array elements


System.out.println("Accessing Elements of Array:");
System.out.println("First Element: " + age[0]);
System.out.println("Second Element: " + age[1]);
System.out.println("Third Element: " + age[2]);
System.out.println("Fourth Element: " + age[3]);
System.out.println("Fifth Element: " + age[4]);
}
}
ACTIVITY 8.0: Loop in an Array sample.
class Main {
public static void main(String[] args) {

// create an array
int[] age = {12, 4, 5};

// loop through the array


// using for loop
System.out.println("Using for Loop:");
for(int i = 0; i < age.length; i++) {
System.out.println(age[i]);
}
}
}

JOPTION PANE ACTIVITY


ACTIVITY 1.

package activity8;

import javax.swing.JOptionPane;
public class Activity8 {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {

String inputString;
String name;
int hours;
double payrate;
double grosspay;
name=JOptionPane.showInputDialog("What is your name: ");
inputString = JOptionPane.showInputDialog("How many hours"+" did you work
this week? ");
hours = Integer.parseInt(inputString);
inputString=JOptionPane.showInputDialog("What is your "+ "Hourly Pay rate?:
");
payrate = Double.parseDouble(inputString);
grosspay =hours*payrate;
JOptionPane.showMessageDialog(null, "Hello "+name+". Your gross pay is Php "
+grosspay);
System.exit(0);
}}
ACTIVITY 2.
package activity9;
import javax.swing.JOptionPane;
public class Activity9 {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {

String username, password;


username = JOptionPane.showInputDialog("Username: ");
password = JOptionPane.showInputDialog("Password: ");
if (username.equals("cprog2")&&password.equals("bsis1a")) {
JOptionPane.showMessageDialog(null,"Thanks! Your are login");
}
else {
JOptionPane.showMessageDialog(null,"Sorry.Username or Password
Incorrect.");
}
System.exit(0);
}

}
ACTIVITY 3.
package activity9_2;

import javax.swing.JOptionPane;
public class Activity9_2 {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
String inputString;
double iqScore;
inputString=JOptionPane.showInputDialog("Please enter your IQ score: ");
iqScore = Double.parseDouble(inputString);
if (iqScore>=130) {
JOptionPane.showMessageDialog(null, iqScore+ " IQ is Intellegent.");
}
else if(iqScore<=129 && iqScore>=120){
JOptionPane.showMessageDialog(null, iqScore+ " IQ is Superior.");
}
else if(iqScore<=119 && iqScore>=110) {
JOptionPane.showMessageDialog(null, iqScore+ " IQ is Above Average.");
}
else if(iqScore<=109 && iqScore>=90) {
JOptionPane.showMessageDialog(null, iqScore+ " IQ is Average.");
}
else if(iqScore<=89 && iqScore>=80) {
JOptionPane.showMessageDialog(null, iqScore+ " IQ is Below Average.");
}
else {
JOptionPane.showMessageDialog(null, iqScore+ "is Poor.");
}

}
}
ACTIVITY 4.
package activity9_3;
import java.io.*;
public class Activity9_3 {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
BufferedReader dataIn = new BufferedReader(new
InputStreamReader(System.in));
String yearLevel="";
System.out.println("Please enter your Year Level \n (Type 1,2,3,or 4): ");

try{
yearLevel=dataIn.readLine();

switch(yearLevel){
case "1": System.out.println("You're a Freshman");
break;
case "2": System.out.println("You're a Sophomore");
break;
case "3": System.out.println("You're a Junior");
break;
case "4": System.out.println("You're a Senior");
break;
default:
System.out.println("Year Level not Matched" );
break;
}
}
catch(IOException e ){
System.out.println("Error in getting input");
}

You might also like