|
@@ -0,0 +1,208 @@ |
|
|
|
|
|
import java.util.Calendar; |
|
|
|
|
|
import java.util.concurrent.ThreadLocalRandom; |
|
|
|
|
|
|
|
|
|
|
|
class runLabs { |
|
|
|
|
|
public static void main(String[] args) { |
|
|
|
|
|
new Lab4(); |
|
|
|
|
|
new Lab5(); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
class Section { |
|
|
|
|
|
private int _sectionNumber; |
|
|
|
|
|
|
|
|
|
|
|
public Section(int s) { |
|
|
|
|
|
this._sectionNumber = s; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public int getSection() { |
|
|
|
|
|
return _sectionNumber; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
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) |
|
|
|
|
|
); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
class Lab5 extends Lab { |
|
|
|
|
|
public Lab5() { |
|
|
|
|
|
super(5); |
|
|
|
|
|
System.out.println(String.format("hi")); |
|
|
|
|
|
} |
|
|
|
|
|
} |