Created
August 21, 2018 07:51
-
-
Save luchtech/d59d5c807177594ce7903f103a0fb914 to your computer and use it in GitHub Desktop.
Laboratory Activity: Selection Structure
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
public class BowlingGame { | |
public static void main(String[] args) { | |
Scanner sc = new Scanner(System.in); | |
System.out.println("Program: BOWLING SCOREBOARD\n"); | |
int score, total1 = 0, total2 = 0; | |
try{ | |
System.out.println("\nFirst Try\n"); | |
System.out.println("Player1 Score:"); | |
score = sc.nextInt(); | |
total1+=score; | |
System.out.println("Player2 Score:"); | |
score = sc.nextInt(); | |
total2+=score; | |
System.out.println("\nSecond Try\n"); | |
System.out.println("Player1 Score:"); | |
score = sc.nextInt(); | |
total1+=score; | |
System.out.println("Player2 Score:"); | |
score = sc.nextInt(); | |
total2+=score; | |
System.out.println("\nThird Try\n"); | |
System.out.println("Player1 Score:"); | |
score = sc.nextInt(); | |
total1+=score; | |
System.out.println("Player2 Score;"); | |
score = sc.nextInt(); | |
total2+=score; | |
System.out.println("\nPlayer 1 Total Score : "+total1); | |
System.out.println("Player 2 Total Score : "+total2); | |
if (total1 > total2) System.out.println("\nCongratulations, Player 1. You won."); | |
else if (total1 < total2) System.out.println("\nCongratulations, Player 2. You won."); | |
else System.out.println("\nDraw!"); | |
} | |
catch (Exception e) { | |
System.out.println("\nInput error!"); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
public class DevelopmentalTask { | |
public static void main(String[] args) { | |
Scanner sc = new Scanner(System.in); | |
System.out.println("Program: Developmental Task Stage\n"); | |
System.out.println("Enter age: "); | |
try { | |
double age = sc.nextDouble(); | |
if (age < 0) | |
System.err.println("\nError: There is no such thing as a negative age!"); | |
else if (age > 60) System.out.println("\nResult:\nLater Maturity"); | |
else if (age >= 36 && age <= 60) System.out.println("\nResult:\nMiddle Age"); | |
else if (age >= 18 && age < 36) System.out.println("\nResult:\nEarly Adulthood"); | |
else if (age >= 13 && age < 18) System.out.println("\nResult:\nAdolescence"); | |
else if (age >= 6 && age < 13) System.out.println("\nResult:\nMiddle Childhood"); | |
else System.out.println("\nResult:\nInfancy and Early Childhood"); | |
} | |
catch(Exception e){ | |
System.err.println("\nError: Invalid input!"); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
public class GradeValidation { | |
public static void main(String[] args) { | |
Scanner sc = new Scanner(System.in); | |
System.out.println("Program: REMARKS FOR GRADES\n"); | |
System.out.println("Note: You can only input numbers between 0 to 100.\n"); | |
System.out.print("Enter grade: "); | |
try { | |
double grade = sc.nextDouble(); | |
if (grade > 100 || grade < 0) | |
System.err.println("\nError: Out of range!"); | |
else if (grade >=90 && grade <= 100) System.out.println("\nRemarks: Excellent"); | |
else if (grade >=85 && grade < 90) System.out.println("\nRemarks: Very Good"); | |
else if (grade >=80 && grade < 85) System.out.println("\nRemarks: Good"); | |
else if (grade >=75 && grade < 80) System.out.println("\nRemarks: Poor"); | |
else System.out.println("\nRemarks: Fail"); | |
} | |
catch(Exception e){ | |
System.err.println("\nError: Invalid input!"); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
public class OddEvenValidation { | |
public static void main(String[] args) { | |
Scanner sc = new Scanner(System.in); | |
System.out.println("Program: ODD-EVEN NUMBER DETECTOR\n"); | |
System.out.println("\nNote: You can only input numbers between\n"+Long.MIN_VALUE+" and"+Long.MAX_VALUE+".\n\n"); | |
System.out.println("Please enter a number:"); | |
try{ | |
long x = sc.nextLong(); | |
if (x % 2 == 0) System.out.println("\nResult:\n"+x+" is EVEN!"); | |
else System.out.println("\nResult:\n"+x+" is ODD!"); | |
} | |
catch (Exception e) { | |
System.out.println("\nInput error! Please input a number and it should be\nwithin the maximum and minimun limits."); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
public class OwnerValidation { | |
public static void main(String[] args) { | |
Scanner sc = new Scanner(System.in); | |
System.out.print("Program: OWNER VALIDATION\n"); | |
System.out.print("\nEnter Owner's Name: "); | |
String owner = sc.next(); | |
System.out.print("Result: "); | |
if(owner.equalsIgnoreCase("mart") || owner.equalsIgnoreCase("anne")) System.out.print("is the owner!"); | |
else System.out.print("is owner of nothing"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
public class Payroll { | |
public static void main(String[] args) { | |
Scanner sc = new Scanner(System.in); | |
double base, total=0; | |
System.out.println("Program: PAYROLL (Weekly)"); | |
System.out.print("\nEnter Base Pay Per Hour ($): "); | |
base = sc.nextFloat(); sc.nextLine(); | |
System.out.print("Enter Hours Worked: "); | |
int hours = sc.nextInt(); sc.nextLine(); | |
if (base < 8 || hours > 60 || base < 0 || hours < 0) { | |
System.err.println("Error! One of the inputs or more violates the requirements."); | |
System.exit(0); | |
} | |
else if (hours <= 40) total = hours * base; | |
else if (hours > 40) total = 40 * base + (hours - 40) * base * 1.5; | |
System.out.printf("Total Wage: $%,.2f",total); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
public class PointGradeValidation { | |
public static void main(String[] args) { | |
Scanner sc = new Scanner(System.in); | |
int sID = 0; | |
double exam1, exam2, exam3, exam4, average = 0, grade = 0; | |
String name, dept, course, subj, remarks = ""; | |
System.out.println("Program: Student Average Grade Calculator\n"); | |
System.out.println("Please fill out the following fields:"); | |
try { // Try-Catch to only allow NUMBERS for Student ID Number | |
System.out.print("Student ID\t\t: "); | |
sID = sc.nextInt(); | |
sc.nextLine(); // This is necessary to position the pointer to the next line | |
} | |
catch(Exception e) { // Error Message in case someone types letters/special characters on Student ID | |
System.err.println("\nError: Student ID should only contain numbers. Please start the program again."); | |
} | |
System.out.print("Name\t\t\t: "); | |
name = sc.nextLine(); | |
System.out.print("Department\t\t: "); | |
dept = sc.nextLine(); | |
System.out.print("Course\t\t\t: "); | |
course = sc.nextLine(); | |
System.out.print("Subject\t\t\t: "); | |
subj = sc.nextLine(); | |
try { // Try-Catch to only allow NUMBERS for Exam Grades | |
System.out.print("First Examination\t: "); | |
exam1 = sc.nextDouble(); | |
System.out.print("Second Examination\t: "); | |
exam2 = sc.nextDouble(); | |
System.out.print("Third Examination\t: "); | |
exam3 = sc.nextDouble(); | |
System.out.print("Final Examination\t: "); | |
exam4 = sc.nextDouble(); | |
/*************** PROCESS ***************/ | |
if ((exam1 < 0 || exam1 > 100)&&(exam2 < 0 || exam2 > | |
100)&&(exam3 < 0 || exam3 > 100)&&(exam4 < 0 || exam4 > 100)) { | |
System.err.println("\nError: One of the grades or more is/are not between 0 and 100!"); | |
System.exit(0); | |
} | |
average = (exam1+exam2+exam3+exam4)/4; // Average of 4 Exams | |
grade = (100-average+10)/10; // Point Grade Formula | |
if (average < 0 || average > 100) | |
System.err.println("\nError: Out of range or invalid"); | |
else if (average == 100) remarks = "Passed-Excellent"; | |
else if (average >= 90 && average < 100) remarks = "Passed-Very Good"; | |
else if (average >= 85 && average < 90) remarks = "Passed-Average"; | |
else if (average >= 80 && average < 85) remarks = "Passed-Good"; | |
else if (average >= 75 && average < 80) remarks = "Passed-Satisfactory"; | |
else if (average >= 50 && average < 75) { | |
remarks = "Failed"; | |
grade = 5.0; | |
} | |
else if (average > 0 && average < 50) { | |
remarks = "Dropped"; | |
grade = 9.0; | |
} | |
else if (average == 0) { | |
remarks = "No such grade"; | |
grade = 0.0; | |
} | |
/*************** OUTPUT ***************/ | |
System.out.println("--------Student Details--------"); | |
System.out.println("ID\t\t: "+sID); | |
System.out.println("Name\t\t: "+name); | |
System.out.println("Department\t: "+dept); | |
System.out.println("Course\t\t: "+course); | |
System.out.println("Subject\t\t: "+subj); | |
System.out.println("---------Grade Details---------"); | |
System.out.println("First\tSecond\tThird\tFourth"); | |
System.out.println(exam1+"\t"+exam2+"\t"+exam3+"\t"+exam4); | |
System.out.printf("Average\t\t: %.2f\n",average); | |
System.out.printf("Point Grade\t: %.2f\n",grade); | |
System.out.println("Remarks\t\t: "+remarks); | |
} | |
catch(Exception e){ // Error Message in case someone types letters/special characters on Exam Scores | |
System.err.println("\nError: Invalid input!"); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
public class PositiveNegative { | |
public static void main(String[] args) { | |
Scanner sc = new Scanner(System.in); | |
System.out.println("Program: POSITIVE-NEGATIVE NUMBER DETECTOR\n"); | |
System.out.println("\nNote: You can only input numbers between\n"+Long.MIN_VALUE+" and"+Long.MAX_VALUE+".\n\n"); | |
System.out.println("Please enter a number:"); | |
try{ | |
long x = sc.nextLong(); | |
if (x == 0) System.out.println("\nResult:\n"+x+" is neither positive nor negative."); | |
else if (x > 0) System.out.println("\nResult:\n"+x+" is POSITIVE!"); | |
else System.out.println("\nResult:\n"+x+" is NEGATIVE!"); | |
} | |
catch (Exception e) { | |
System.out.println("\nInput error! Please input a number and it should bewithin the maximum and minimun limits."); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
public class TrafficLight { | |
public static void main(String[] args) { | |
Scanner sc = new Scanner(System.in); | |
System.out.println("Program: TRAFFIC LIGHT COLORS\n"); | |
System.out.println("Available Colors: Green, Orange, Red"); | |
System.out.print("\nEnter color: "); | |
String color = sc.next(); | |
System.out.print("Result: "); | |
if(color.equalsIgnoreCase("green")) System.out.print("Go!"); | |
else if(color.equalsIgnoreCase("orange")) System.out.print("Ready!"); | |
else if(color.equalsIgnoreCase("red")) System.out.print("Stop!"); | |
else System.out.print("That color does not belong to the traffic light colors specified above."); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
public class UserAccountValidation { | |
public static void main(String[] args) { | |
Scanner sc = new Scanner(System.in); | |
System.out.print("Program: Simple Calculator with Login\n"); | |
System.out.print("\nEnter username\t: "); | |
String user = sc.next(); sc.nextLine(); | |
System.out.print("Enter password\t: "); | |
String pass = sc.next(); sc.nextLine(); | |
if (user.equalsIgnoreCase("admin") && pass.equalsIgnoreCase("pwd12345")) { | |
int num1, num2; | |
char choice; | |
System.out.println("\nProgram: Simple Calculator\n"); | |
System.out.print("(1) Enter first number: "); | |
num1 = sc.nextInt(); | |
sc.nextLine(); | |
System.out.print("(2) Enter second number: "); | |
num2 = sc.nextInt(); | |
System.out.print("(3) Operations:\n[A]dd\n[S]ubtract\n[M]ultiply\n[D]ivide\n[Q]uit\n\nChoose operation to use: "); | |
choice = sc.next().toLowerCase().charAt(0); | |
switch(choice) { | |
case 'a': System.out.println("\nResult: "+(num1 + num2)); break; | |
case 's': System.out.println("\nResult: "+(num1 - num2)); break; | |
case 'm': System.out.println("\nResult: "+(num1 * num2)); break; | |
case 'd': System.out.println("\nResult: "+((double)(num1) / num2)); break; | |
default: System.out.println("\nProgram terminating…"); | |
} | |
} | |
else System.err.println("Invalid username and password combination!"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment