Project 1 - 2
Project 1
package ticketTester;
import javax.swing.*;
public class Main {
public static void main(String[] args) {
int entryAge = 18;
int peopleToTest = 5;
int testsDone = 0;
int allowedIn = 0;
while(testsDone int age = Integer.parseInt(JOptionPane.showInputDialog("Please enter the age of person " +(testsDone + 1)));
if(age System.out.println(age + ": No Entry");
}else{
System.out.println(age + ": Entry Allowed");
allowedIn++;
}
testsDone++;
}
System.out.println((peopleToTest-allowedIn) + " are not allowed entry" );
System.out.println(allowedIn + " are allowed entry");
System.out.println(((Double.parseDouble("" + allowedIn)/testsDone)*100) + "% made it into the program");
}
}
I used an if/else statement rather than 2 if statements. I used a while loop like it said in the task but I would have probably used a for loop otherwise, as we know that it will be running a pre determined number of times. I would use the while loop if we were running an unknown length of time (eg. until 5 people were allowed entry.
Also I left the % as a double rather than parseing it back into an int but it would be easy to do to get rid of the decimal point.
Project 2
package ProjectTwo;
import javax.swing.*;
public class Main {
public static void main(String[] args) {
// write your code here
String name1 = JOptionPane.showInputDialog("Please enter a Name").toUpperCase();
String sName1 = JOptionPane.showInputDialog("Please enter a Surame").toUpperCase();
int age1 = Integer.parseInt(JOptionPane.showInputDialog("Please enter an age"));
while(age1 19) {
age1 = Integer.parseInt(JOptionPane.showInputDialog("That is too young. Please enter an age greater than 19."));
}
String name2 = JOptionPane.showInputDialog("Please enter a Name").toUpperCase();
String sName2 = JOptionPane.showInputDialog("Please enter a Surame").toUpperCase();
int age2 = Integer.parseInt(JOptionPane.showInputDialog("Please enter an age"));
while(age2 19) {
age2 = Integer.parseInt(JOptionPane.showInputDialog("That is too young. Please enter an age greater than 19."));
}
if(sName1.compareTo(sName2) 0){
System.out.println(name1.charAt(0) + ". " + sName1);
System.out.println(age1);
System.out.println(name2.charAt(0) + ". " + sName2);
System.out.println(age2);
}else{
System.out.println(name2.charAt(0) + ". " + sName2);
System.out.println(age2);
System.out.println(name1.charAt(0) + ". " + sName1);
System.out.println(age1);
}
}
}
This feels like a slightly Clunky way to do the program but it does work