This project was for the University of Hawai’i at Mānoa ICS 211 (Introduction to Computer Science II) course during the Spring 2024 term. ICS 211 is a course that teaches students to reinforce and strengthen problem-solving skills using abstract data types. It also introduces software development practices and emphasizes the use of searching and sorting algorithms and their complexity, recursion, object-oriented programming, and data structures.
The assignment had two parts that needed to be completed. The first part was to create a class that could read files and return the amount of words in the file. The second part was to create a class that could return the date and time based on the number of seconds that had passed since the start of the year. The assignment was meant to reinforce our knowledge of Java and how to implement classes and methods to solve a problem.
Solving the problem given by the assignment required me to create two different classes, each meant to solve one part of the problem. The first class I created was the Reader class that used the java.io.file package to be able to access a file along with the Scanner class to be able to read the file. The Reader class used a method to access a file and count the number of words the file has using the scanner class.
public int numWords (String fileName) { //Reader method
try (Scanner scanner = new Scanner(new File(fileName))) { //Try/Catch exception for File Not Found error
int count = 0;
while (scanner.hasNext()) { //Loop for scanner to count words
scanner.next();
count++;
}
return count;
} catch (FileNotFoundException e) { //File Not Found Exception
return -1;
}
}
The second class I created was the Dates class. The Dates class used a boolean to determine if the year in question was a leap year. It then uses two different arrays to determine which month would coincide with the number of seconds that had passed during the year and to determine the date. After the date and time were calculated, the class used a string and formatted it to display the date and time in the proper format (Month Day Hour:Minute:Second).
public String computeDateTime(long sec, boolean leapYear) { //Method to compute date and time
if (sec < 0 || sec >= (leapYear ? 31622400 : 31536000)) {
return "Illegal number of seconds"; //If seconds is negative or exceeds 1 year
}
int[] daysInMonth = {31, (leapYear ? 29 : 28), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; //Days in each month
long secInDay = 86400; //Seconds in one day
long day = sec / secInDay + 1; //Calculating one day
long remain = sec % secInDay; //Calculating remaining seconds after computing number of days
int month = 0, hour = 0, min = 0, second = 0; //Setting counters for month, hour, minutes and seconds to zero
while(day > daysInMonth[month]) { //Using number of days to find month
day -= daysInMonth[month];
month++;
}
hour = (int)(remain / 3600); //Calculating hours
min = (int)(remain % 3600 / 60); //Calculating minutes
second = (int)(remain % 3600 % 60); //Calculating seconds
return String.format("%3s %d %02d:%02d:%02d", new String[] {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}[month], day, hour, min, second); //Formatting date and time
}
Due to the assignment, I learned how to effectively create methods that can provide a solution to a specific problem. The assignment also reinforced my knowledge of Java data structures and how to implement different classes and methods to solve a problem. This project made me more confident in my skills as a complex thinker and problem solver.
Source: jseto808/ICS211-A1