0% found this document useful (0 votes)
36 views

Comp Ulet

The document contains examples of switch statements in Java that demonstrate fall through behavior without breaks, calculating days in a month, and mapping grades to results. It also includes examples of using a for loop with a Scanner to calculate a sum and total costs from user input. Methods are used to process employee work hours and calculate a total. The examples show different uses of switch statements to select cases and execute code blocks based on various conditions like month number or character grade. For loops iterate from 1 to a given number or days to collect input and calculate running totals. Methods allow breaking down the work hours calculation into reusable chunks. Overall the document provides multiple code snippets demonstrating

Uploaded by

Arabella Sanchez
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)
36 views

Comp Ulet

The document contains examples of switch statements in Java that demonstrate fall through behavior without breaks, calculating days in a month, and mapping grades to results. It also includes examples of using a for loop with a Scanner to calculate a sum and total costs from user input. Methods are used to process employee work hours and calculate a total. The examples show different uses of switch statements to select cases and execute code blocks based on various conditions like month number or character grade. For loops iterate from 1 to a given number or days to collect input and calculate running totals. Methods allow breaking down the work hours calculation into reusable chunks. Overall the document provides multiple code snippets demonstrating

Uploaded by

Arabella Sanchez
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/ 4

MISLANG KASSANDRA KAY

SWITCH STATEMENTS
public class SwitchDemoFallThrough {

public static void main(String[] args) {


java.util.ArrayList<String> futureMonths =
new java.util.ArrayList<String>();

int month = 8;

switch (month) {
case 1: futureMonths.add("January");
case 2: futureMonths.add("February");
case 3: futureMonths.add("March");
case 4: futureMonths.add("April");
case 5: futureMonths.add("May");
case 6: futureMonths.add("June");
case 7: futureMonths.add("July");
case 8: futureMonths.add("August");
case 9: futureMonths.add("September");
case 10: futureMonths.add("October");
case 11: futureMonths.add("November");
case 12: futureMonths.add("December");
break;
default: break;
}

if (futureMonths.isEmpty()) {
System.out.println("Invalid month number");
} else {
for (String monthName : futureMonths) {
System.out.println(monthName);
}
}
}
}

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

int month = 2;
int year = 2000;
int numDays = 0;

switch (month) {
case 1: case 3: case 5:
case 7: case 8: case 10:
case 12:
MISLANG KASSANDRA KAY

numDays = 31;
break;
case 4: case 6:
case 9: case 11:
numDays = 30;
break;
case 2:
if (((year % 4 == 0) &&
!(year % 100 == 0))
|| (year % 400 == 0))
numDays = 29;
else
numDays = 28;
break;
default:
System.out.println("Invalid month.");
break;
}
System.out.println("Number of Days = "
+ numDays);
}
}

public class SwitchCaseExample {


public static void main(String[] args) {

grading('A');
grading('C');
grading('E');
grading('G');
}public static void grading(char grade) {
int success;
switch (grade) {
case 'A':
System.out.println("Excellent grade");
success = 1;
break;
case 'B':
System.out.println("Very good grade");
success = 1;
break;
case 'C':
System.out.println("Good grade");
success = 1;
break;
case 'D':
case 'E':
case 'F':
System.out.println("Low grade");
success = 0;
break;
default:
MISLANG KASSANDRA KAY

System.out.println("Invalid grade");
success = -1;
break;
}
passTheCourse(success);
}
public static void passTheCourse(int success) {
switch (success) {
case -1:
System.out.println("No result");
break;
case 0:
System.out.println("Final result: Fail"); break;
case 1:
System.out.println("Final result: Success");
break;
default:
System.out.println("Unknown result");
break;
}
}

FOR LOOP USING SCANNER


import java.util.Scanner
public class AddingValuesWithAForLoop
{
public static void main( String[] args )
{
Scanner keyboard = new Scanner(System.in);
System.out.println( " \n" );

System.out.println( "Number: " );


int number = keyboard.nextInt();
int sum = 0;

for (int run=1; run<=number; run=run+1)


{
System.out.print( run + " " );
sum = sum + 1 ;
}

System.out.println( "The sum is . " );


}
}

import java.util.*;
public class Receipt2 {
public static void main(String[] args)
{ Scanner console = new Scanner(System.in);
System.out.print("How many people ate? ");
int people = console.nextInt();
double subtotal = 0.0; // cumulative sum
for (int i = 1; i <= people; i++) {
System.out.print("Person #" + i + ": How much did your dinner cost?
");
MISLANG KASSANDRA KAY

double personCost = console.nextDouble();


subtotal = subtotal + personCost; // add to sum } results(subtotal);
} // Calculates total owed, assuming 8% tax and 15% tip
public static void results(double subtotal)
{ double tax = subtotal * .08; double tip = subtotal * .15; double
total = subtotal + tax + tip; System.out.printf("Subtotal: $%.2f\n",
subtotal); System.out.printf("Tax: $%.2f\n", tax);
System.out.printf("Tip: $%.2f\n", tip); System.out.printf("Total:
$%.2f\n", total); }
}

// Computes the total paid hours worked by two employees.


// The company does not pay for more than 8 hours per day.
// Uses a "cumulative sum" loop to compute the total hours.
import java.util.*;
public class Hours {
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
int hours1 = processEmployee(console, 1);
int hours2 = processEmployee(console, 2);
int total = hours1 + hours2;
System.out.println("Total hours for both = " + total); } . // Reads hours
information about an employee with the given number. // Returns total hours
worked by the employee.
public static int processEmployee
(Scanner console, int number) {
System.out.print("Employee " + number + ": How many days? ");
int days = console.nextInt(); // totalHours is a cumulative sum of all days'
hours worked.
int totalHours = 0; for (int i = 1; i <= days; i++) {
System.out.print("Hours? ");
int hours = console.nextInt();
totalHours = totalHours + Math.min(hours, 8); }
double hoursPerDay = (double) totalHours / days;
System.out.printf("Employee %d's total hours = %d (%.1f / day)\n", number,
totalHours, hoursPerDay); System.out.println(); return totalHours;
}
}

You might also like