|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342 |
- import java.util.Calendar;
- import java.util.Scanner;
- import java.util.concurrent.ThreadLocalRandom;
-
- class runLabs {
- public static void main(String[] args) {
- new Lab4();
- new Lab5();
- new Lab6();
- }
- }
-
- class Section {
- private int _sectionNumber;
-
- public Section(int s) {
- this._sectionNumber = s;
- this.printSection();
- }
-
- public int getSection() {
- return _sectionNumber;
- }
-
- public void printSection() {
- String title = String.format("Starting section %s", this._sectionNumber);
-
- System.out.println("-".repeat(title.length()));
- System.out.println(String.format("%s", title));
- System.out.println("-".repeat(title.length()));
- }
- }
-
- class Lab {
-
- private int _labNumber;
- public Section currentSection;
-
- public Section getCurrentSection() {
- return currentSection;
- }
-
- public void setCurrentSection(Section currentSection) {
- this.currentSection = currentSection;
- }
-
- public static void printSection(String section) {
- String title = String.format("Starting section %s", section);
-
- System.out.println("-".repeat(title.length()));
- System.out.println(String.format("%s", title));
- System.out.println("-".repeat(title.length()));
- }
-
- public Lab(int lN) {
- String title = String.format("|Starting Lab %s|", lN);
- this._labNumber = lN;
-
- System.out.println("*".repeat(title.length()));
- System.out.println(title);
- System.out.println("*".repeat(title.length()));
- }
-
- public int getLabNumber() {
- return this._labNumber;
- }
- }
-
- class Lab4 extends Lab {
- public Lab4() {
- super(4);
- ex44();
- ex46();
- }
-
- // public static void main(String[] args) {
- // }
-
- public static void ex41() {
- String labSection = "4.1";
- // System.out.println(String.format("Starting section %s", labSection));
- printSection(labSection);
-
- int testYear1 = 1996; // true
- int testYear2 = 1997; // false
- int testYear3 = 2000; // true
- int testYear4 = 9332; // true
- int testYear5 = 1900; // false
-
- int randomYear = ThreadLocalRandom.current().nextInt(1, 9999 + 1);
-
- System.out.println(String.format("Testing %s", testYear1));
- System.out.println(String.format("%s", isLeap(testYear1)));
-
- System.out.println(String.format("Testing %s", testYear2));
- System.out.println(String.format("%s", isLeap(testYear2)));
-
- System.out.println(String.format("Testing %s", testYear3));
- System.out.println(String.format("%s", isLeap(testYear3)));
-
- System.out.println(String.format("Testing %s", testYear4));
- System.out.println(String.format("%s", isLeap(testYear4)));
-
- System.out.println(String.format("Testing %s", randomYear));
- System.out.println(String.format("%s", isLeap(randomYear)));
-
- System.out.println(String.format("Testing %s", testYear5));
- }
-
- public static void ex44() {
- String labSection = "4.4";
- printSection(labSection);
-
- // System.out.println("------------------------");
- // System.out.println("Starting lab section " + labSection);
- // System.out.println("------------------------");
-
- String month1 = "oct";
- int year1 = 2000;
- System.out.println(String.format("Month: %s\tYear: %d\tDays in month: %d",
- month1, year1, monthLen(month1, year1)));
-
- String month2 = "feb";
- int year2 = 2000;
- System.out.println(String.format("Month: %s\tYear: %d\tDays in month: %d",
- month2, year2, monthLen(month2, year2)));
-
- String month3 = "feb";
- int year3 = 1997;
- System.out.println(String.format("Month: %s\tYear: %d\tDays in month: %d",
- month3, year3, monthLen(month3, year3)));
-
- String month4 = "apr";
- int year4 = 2001;
- System.out.println(String.format("Month: %s\tYear: %d\tDays in month: %d",
- month4, year4, monthLen(month4, year4)));
-
- }
-
- /**
- * Lab section 4.5
- *
- * Write a program that can calculate how many whole years old someone is from
- * today’s date and their date of birth. You could store each date as three
- * integers (day,
- * month, year)
- */
- public static void ex46() {
- String labSection = "4.6";
- printSection(labSection);
- // calculate whole years' old to date
-
- Calendar today = Calendar.getInstance();
- System.out.println("Today:\t" + today.getTime());
-
- Calendar dob1 = Calendar.getInstance();
- dob1.set(1990, 1, 1);
-
- System.out.println(String.format(
- "DOB 1:\t%s (difference:\t%d years)",
- dob1.getTime(),
- yearDifference(today, dob1)));
-
- Calendar dob2 = Calendar.getInstance();
- dob2.set(1994, 12, 22);
-
- System.out.println(String.format(
- "DOB 2:\t%s (difference:\t%d years)",
- dob2.getTime(),
- yearDifference(today, dob2)));
- }
-
- public static boolean isLeap(int year) {
- if (year % 4 != 0) {
- return false;
- } else {
- if (year % 100 == 0) {
- if (year % 400 == 0) {
- return true;
- } else {
- return false;
- }
- }
- return true;
- }
- }
-
- public static int monthLen(String month, int year) {
- if (month == "feb") {
- if (isLeap(year)) {
- return 29;
- } else {
- return 28;
- }
- } else if (
- (month == "apr") ||
- (month == "jun") ||
- (month == "sep") ||
- (month == "nov")) {
- return 30;
- } else {
- return 31;
- }
- }
-
- public static int yearDifference(Calendar date1, Calendar date2)
- {
- return (int) Math.abs(date1.get(Calendar.YEAR) -
- date2.get(Calendar.YEAR)
- );
- }
- }
-
- /**
- * Keyboard input
- */
- class Lab5 extends Lab {
- public Lab5() {
- super(5);
- new five1();
- }
-
- class five1 extends Section {
- /**
- * We will now look at how we can get user input from the keyboard. We will
- * gloss over some
- * of the details here, but give you just the information you need to start
- * getting input and
- * processing it.
- */
- public five1() {
- super(1);
- System.out.println(String.format("TBC"));
- // Scanner keyboard = new Scanner(System.in);
- // System.out.println("Please enter your name, followed by the return key?");
- // String userEntry = keyboard.nextLine();
- // System.out.println("Hello " + userEntry);
- // System.out.println(String.format("%s", keyboard));
- }
- }
- }
-
- /**
- * Loops
- */
- class Lab6 extends Lab {
- public Lab6() {
- super(6);
- new six1();
- System.out.println(String.format("%n"));
- new six3();
- new six5();
- }
-
- /**
- * Times tables
- */
- class six1 extends Section {
- public six1() {
- super(1);
- int testNum = 6;
- System.out.println(String.format("Times table for %d:", testNum));
- writeTimesTable(7);
- }
- }
-
- class six3 extends Section {
- public six3() {
- super(3);
- int testNum = 13; // statistically the most prime of all numbers
- System.out.println(String.format("Testing primes..."));
- System.out.println(String.format("%d prime? %s", testNum, isPrime(testNum)));
- System.out.println(String.format("%d prime? %s", 23, isPrime(23)));
- System.out.println(String.format("%d prime? %s", 27, isPrime(27)));
- System.out.println(String.format("%d prime? %s", 28, isPrime(28)));
- System.out.println(String.format("%d prime? %s", 299, isPrime(299)));
- System.out.println(String.format("Generating primes..."));
- generatePrimes(1024);
- System.out.println(String.format("Done!%n"));
- }
- }
-
- class six5 extends Section {
- public six5() {
- super(5);
- System.out.println(String.format("TODO / NOT IMPLEMENTED until we get stdin working"));
- }
- }
-
- public void writeTimesTable(int num, int limit) {
- for (int i = 0; i < (limit + 1); i++) {
- System.out.printf("%d\t", num * i);
- }
- }
-
- public void writeTimesTable(int num) {
- int DEFAULT_LIMIT = 10;
- writeTimesTable(num, DEFAULT_LIMIT);
- }
-
- public void generatePrimes(int upperLimit) {
- // handle 2 as a special case
- System.out.println(String.format("2"));
- // for (int i = 3; i <= upperLimit; i += 2) {
- for (int i = 2; i <= upperLimit; i++) {
- // TODO: figure out smarter way of doing this, I'm sure primes must be at least 6 apart...
- // Something about 2n ± 1 ?
- // seive of Erasthotenes ..?
- if (isPrime(i)) {
- System.out.printf("%d\t", i);
- }
- }
- }
-
- /**
- * But is it prime?
- * @param num Integer to test for primality
- * @return if it's prime
- */
- public boolean isPrime(int num) {
- boolean DEBUG = false;
- if ((num == 2) || (num == 1)) {
- return true;
- }
- if (num % 2 == 0){
- return false;
- } else {
- int divisor = 3;
- double numSquareRoot = Math.sqrt((double)num);
- while (divisor <= numSquareRoot) {
- if (num % divisor == 0) {
- return false;
- }
- divisor += 2;
- if (DEBUG) {
- System.out.printf("%d\t", divisor);
- }
- }
- return true;
- }
- }
- }
|