FINALS Lab Activity 4-7
FINALS Lab Activity 4-7
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.
// 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: " + _______);
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.");
}
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;
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;
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();
}
}
// create an array
int[] age = {12, 4, 5, 2, 5};
// create an array
int[] age = {12, 4, 5};
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) {
}
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");
}