Created
March 7, 2019 20:51
-
-
Save jaohaohsuan/fedbb8161c4f1166fb98ae41cc91a500 to your computer and use it in GitHub Desktop.
week3 lab
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
package comp601.assignment1; | |
import static java.lang.System.out; | |
import java.util.*; | |
public class Main { | |
private static Scanner input = new Scanner(System.in); | |
public static void main(String[] args) { | |
out.printf("This is the assignment one demostration.\n\nStudent: Henry Jao.\n"); | |
while(true) { | |
out.print("\nPlease enter the question number: "); | |
String userInput = input.nextLine(); | |
// separation new line | |
out.println(); | |
switch(userInput) { | |
case "1": | |
question1(); | |
break; | |
case "2": | |
question2(); | |
break; | |
case "3": | |
question3(); | |
break; | |
case "4": | |
question4(); | |
break; | |
case "5": | |
question5(); | |
break; | |
case "e": | |
out.println("Program terminated."); | |
System.exit(0); | |
break; | |
default: | |
out.println("Invalid input!"); | |
} | |
} | |
} | |
// week 1 lab | |
public static void question1() { | |
//create two fixed length column | |
String columnFmt = String.format("%%-%ds%%-%ds\n",30, 30); | |
out.printf(columnFmt, "Escape sequence", "Description"); | |
out.printf(columnFmt, dash(20), dash(20)); | |
out.printf(columnFmt, "\\n", "New line character"); | |
out.printf(columnFmt, "\\t", "Horizontal tab character"); | |
out.printf(columnFmt, "\\\"", "Double quotes character"); | |
} | |
private static String dash(int len) { | |
if(len > 0) { | |
return new String(new char[len]).replace('\0', '-'); | |
} else { | |
return ""; | |
} | |
} | |
// questions | |
// week 2 lab | |
public static void question2() { | |
out.print("Enter circle radius: "); | |
Double radius = 0.0; | |
try { | |
// read radius value from user input | |
radius = Double.parseDouble(input.nextLine()); | |
} catch (Exception ex) { | |
out.println("invalid radius input"); | |
out.printf("Exception type: %s\n",ex.getClass()); | |
return; | |
} | |
Double diameter = radius * 2; | |
Double perimeter = diameter * Math.PI; | |
Double circleArea = Math.PI * radius * radius; | |
String columns = String.format("%-12s%-12s%12s", "Radius", "Perimeter", "Area"); | |
String results = String.format("%-12s%-12.2f%12.2f", radius, perimeter, circleArea); | |
out.println(columns); | |
out.println(dash(columns.length())); | |
out.println(results); | |
} | |
// week 3 lab | |
public static void question3() { | |
Double score = 0.0; | |
while(true) { | |
out.print("Enter a score: "); | |
String userInput = input.nextLine().trim(); | |
Boolean validNumber = userInput.matches("\\d*(.)?\\d*"); | |
if(validNumber) { | |
score = Double.parseDouble(userInput); | |
String result = scoreRange(score); | |
if( result == "") { | |
out.println("Invalid score"); | |
} else { | |
out.printf("The grade is %s.\n", result); | |
break; | |
} | |
} else { | |
out.println("Invalid score input"); | |
} | |
} | |
} | |
private static String scoreRange(Double value) { | |
if(value >=0 && value < 50) { | |
return "D"; | |
} | |
else if(value >= 50 && value < 70) { | |
return "C"; | |
} | |
else if(value >= 70 && value < 80) { | |
return "B"; | |
} | |
else if(value >= 80 && value <= 100) { | |
return "A"; | |
} | |
else { | |
return ""; | |
} | |
} | |
// week 3 lab | |
public static void question4() { | |
String nameOfCard = ""; | |
String inputLetter = ""; | |
while(nameOfCard == "") { | |
out.print("Enter a card letter: "); | |
inputLetter = input.nextLine().trim(); | |
switch(inputLetter.toLowerCase()) { | |
case "j": | |
nameOfCard = "Jack"; | |
break; | |
case "q": | |
nameOfCard = "Queen"; | |
break; | |
case "k": | |
nameOfCard = "King"; | |
break; | |
case "a": | |
nameOfCard = "Ace"; | |
break; | |
default: | |
out.printf("\"%s\" is not a valid card letter. Try again!\n", inputLetter); | |
// keep looping | |
} | |
} | |
out.printf("%s corresponds to %s.\n", inputLetter, nameOfCard); | |
} | |
public static void question5() { | |
int numOfSensors = 0; | |
while(true) { | |
out.printf("Enter number of sensors: "); | |
String userInput = input.nextLine().trim(); | |
if(userInput.matches("\\d*")) { | |
numOfSensors = Integer.parseInt(userInput); | |
break; | |
} | |
} | |
double[] temeratureValues = new double[numOfSensors]; | |
int counter = 0; | |
while(counter < numOfSensors) { | |
out.printf("Enter tempture #%d: ", counter+1); | |
String userInput = input.nextLine().trim(); | |
Boolean validNumber = userInput.matches("\\d*(.)?\\d*"); | |
if(validNumber) { | |
temeratureValues[counter] = Double.parseDouble(userInput); | |
counter++; | |
} | |
} | |
Double sum = 0.0; // use for sum the temeratureValues | |
Double max = -Double.MAX_VALUE; // use for max value of temeratureValues and initial with smallest value of double | |
StringBuilder numBtw10to20 = new StringBuilder(); | |
for(int i = 0; i < temeratureValues.length; i++) { | |
sum += temeratureValues[i]; | |
if(max < temeratureValues[i]) { | |
max = temeratureValues[i]; | |
} | |
if(temeratureValues[i] >= 10 && temeratureValues[i] <= 20) { | |
numBtw10to20.append(String.format("%d, ", i + 1)); | |
} | |
} | |
Double avgOfTemperatureValues = sum / temeratureValues.length; | |
out.printf("Average Temperature: %.2f\n", avgOfTemperatureValues); | |
out.printf("Maximum Temperature: %.2f\n", max); | |
// remove dangling chars | |
String numBtw10to20Result = numBtw10to20.toString(); | |
if(numBtw10to20Result.length() >= 3) { | |
numBtw10to20Result = numBtw10to20Result.substring(0, numBtw10to20Result.length()-2); | |
} | |
out.printf("Number of temperature between 10-20: %s",numBtw10to20Result); | |
} | |
// private static String dropDanglingString(String src, String dropChars) { | |
// src.ma | |
// return ""; | |
// } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment