SlideShare a Scribd company logo
public class Storm {
//Attributes
private String stormName;
private int stormYear;
private String stormStart;
private String stormEnd;
private int stormMag;
/**
* Constructor
*
* @param stormName
* @param stormYear
* @param stormStart
* @param stormEnd
* @param stormMag
*/
public Storm(String stormName, int stormYear, String stormStart, String stormEnd, int
stormMag) {
this.stormName = stormName;
this.stormYear = stormYear;
this.stormStart = stormStart;
this.stormEnd = stormEnd;
this.stormMag = stormMag;
}
/**
* @return the stormName
*/
public String getStormName() {
return stormName;
}
/**
* @param stormName the stormName to set
*/
public void setStormName(String stormName) {
this.stormName = stormName;
}
/**
* @return the stormYear
*/
public int getStormYear() {
return stormYear;
}
/**
* @param stormYear the stormYear to set
*/
public void setStormYear(int stormYear) {
this.stormYear = stormYear;
}
/**
* @return the stormStart
*/
public String getStormStart() {
return stormStart;
}
/**
* @param stormStart the stormStart to set
*/
public void setStormStart(String stormStart) {
this.stormStart = stormStart;
}
/**
* @return the stormEnd
*/
public String getStormEnd() {
return stormEnd;
}
/**
* @param stormEnd the stormEnd to set
*/
public void setStormEnd(String stormEnd) {
this.stormEnd = stormEnd;
}
/**
* @return the stormMag
*/
public int getStormMag() {
return stormMag;
}
/**
* @param stormMag the stormMag to set
*/
public void setStormMag(int stormMag) {
this.stormMag = stormMag;
}
@Override
public String toString() {
return " " + getStormYear() + ": " + getStormName() + " " +
((getStormMag() == -1) ? "(no info)" :
((getStormMag() == 0) ? "(tropical storm)" : "(hurricane level " +
getStormMag() + ")")) + ": " +
((getStormStart().equals("")) ? "(no start)" : getStormStart().substring(0, 2) + "/"
+ getStormStart().substring(2)) +
" - " +
((getStormEnd().equals("")) ? "(no end)" : getStormEnd().substring(0, 2) + "/" +
getStormEnd().substring(2));
}
}
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Database {
private static final int MAX_SIZE = 50;
//Attributes
private Storm[] stormArr;
private int count;
/**
* Constructor
* Accepts a file and attempts to read it
* Fills the storm array with the data
*/
public Database(File fileName) {
//Initialize array
this.stormArr = new Storm[MAX_SIZE];
this.count = 0;
//Scanner to read from the file
Scanner in = null;
try {
in = new Scanner(fileName);
//Read data from the file
while(in.hasNextLine()) {
//Year of storm/ Name of storm/ mmdd storm started/ mmdd storm ended/ magnitude
of storm
String line = in.nextLine();
String[] data = line.replaceAll("/", "/ ").split("/");
if(data.length < 5)
System.out.println("Database entry not in the correct format: " + line);
//Add data to array
this.stormArr[this.count] = new Storm(data[1].trim(), Integer.parseInt(data[0].trim()),
data[2].trim(), data[3].trim(),
(data[4].trim().equals("")) ? -1 : Integer.parseInt(data[4].trim()));
this.count += 1;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
//Close scanner
if(in == null)
in.close();
}
}
/**
* Returns a storm array which matches the name
*
* @param name
*/
public void getStormsByName(String name) {
boolean found = false;
for (Storm storm : stormArr) {
if((storm != null ) && (storm.getStormName().equalsIgnoreCase(name))) {
found = true;
System.out.print(storm);
}
}
if(!found)
System.out.println("Could not find any storm named "" + name + "".");
}
/**
* Returns a storm array which matches the year
*
* @param year
*/
public void getStormsByYear(int year) {
boolean found = false;
for (Storm storm : stormArr) {
if((storm != null ) && (storm.getStormYear() == year)) {
found = true;
System.out.print(storm);
}
}
if(!found)
System.out.println("Could not find any storm in the year "" + year + "".");
}
/**
* Prints all storm details
*/
public void printAll() {
if(this.stormArr.length == 0)
System.out.println("No data.");
else {
for (Storm storm : stormArr) {
if(storm != null)
System.out.print(storm);
}
}
}
}
import java.io.File;
import java.util.Scanner;
public class Prog1 {
/**
* Displays a list of commands
*/
public static void printCommands() {
System.out.println(" Current available commands:");
System.out.println("1 --> Search for a storm name");
System.out.println("2 --> Search for a year");
System.out.println("3 --> Print all storms");
System.out.println("9 --> Exit");
}
public static void main(String args[]) {
if (args.length == 0) // Check if file name is provided in the command
// line
System.out.println("File name not specified");
else {
File f = new File(args[0]);
if (!f.exists()) // Check if file exists
System.out.println("Input file does not exist.");
else if (f.length() == 0) {
System.out.println("No data in the file");
} else {
// Create Database object
Database db = new Database(f);
// Scanner to get user input
Scanner in = new Scanner(System.in);
// Variable to get user command
int cmd = 0;
//Start
System.out.println("Welcome to the CS-102 Storm Tracker Program");
while (true) {
printCommands();
System.out.print("Your choice? ");
cmd = in.nextInt();
in.nextLine(); //Clear keyboard buffer
switch (cmd) {
case 1: //Search storm by name
System.out.println("Enter storm name prefix: ");
String name = in.nextLine();
db.getStormsByName(name);
break;
case 2: //Search storm by year
System.out.println("Enter storm year: ");
int year = in.nextInt();
db.getStormsByYear(year);
break;
case 3: //Print all storms
db.printAll();
break;
case 9: //Exit
in.close();
System.exit(0);
default:
System.out.println("Invalid command. Please try again.");
}
System.out.println();
}
}
}
}
}
SAMPLE INPUT:
2004/Ali///
2003/Bob///
1980/Sarah/0123/0312/0
1956/Michael/1211/1223/4
1988/Ryan/0926/1019/
1976/Tim/0318/1010/0
2006/Ronald/0919/1012/2
1996/Mona/0707/0723/1
2000/Kim/0101/0201/1
2000/Jim/1101//3
SAMPLE OUTPUT:
Welcome to the CS-102 Storm Tracker Program
Current available commands:
1 --> Search for a storm name
2 --> Search for a year
3 --> Print all storms
9 --> Exit
Your choice? 3
2004: Ali (no info): (no start) - (no end)
2003: Bob (no info): (no start) - (no end)
1980: Sarah (tropical storm): 01/23 - 03/12
1956: Michael (hurricane level 4): 12/11 - 12/23
1988: Ryan (no info): 09/26 - 10/19
1976: Tim (tropical storm): 03/18 - 10/10
2006: Ronald (hurricane level 2): 09/19 - 10/12
1996: Mona (hurricane level 1): 07/07 - 07/23
2000: Kim (hurricane level 1): 01/01 - 02/01
2000: Jim (hurricane level 3): 11/01 - (no end)
Current available commands:
1 --> Search for a storm name
2 --> Search for a year
3 --> Print all storms
9 --> Exit
Your choice? 1
Enter storm name prefix:
Ali
2004: Ali (no info): (no start) - (no end)
Current available commands:
1 --> Search for a storm name
2 --> Search for a year
3 --> Print all storms
9 --> Exit
Your choice? 2
Enter storm year:
2000
2000: Kim (hurricane level 1): 01/01 - 02/01
2000: Jim (hurricane level 3): 11/01 - (no end)
Current available commands:
1 --> Search for a storm name
2 --> Search for a year
3 --> Print all storms
9 --> Exit
Your choice? 9
Solution
public class Storm {
//Attributes
private String stormName;
private int stormYear;
private String stormStart;
private String stormEnd;
private int stormMag;
/**
* Constructor
*
* @param stormName
* @param stormYear
* @param stormStart
* @param stormEnd
* @param stormMag
*/
public Storm(String stormName, int stormYear, String stormStart, String stormEnd, int
stormMag) {
this.stormName = stormName;
this.stormYear = stormYear;
this.stormStart = stormStart;
this.stormEnd = stormEnd;
this.stormMag = stormMag;
}
/**
* @return the stormName
*/
public String getStormName() {
return stormName;
}
/**
* @param stormName the stormName to set
*/
public void setStormName(String stormName) {
this.stormName = stormName;
}
/**
* @return the stormYear
*/
public int getStormYear() {
return stormYear;
}
/**
* @param stormYear the stormYear to set
*/
public void setStormYear(int stormYear) {
this.stormYear = stormYear;
}
/**
* @return the stormStart
*/
public String getStormStart() {
return stormStart;
}
/**
* @param stormStart the stormStart to set
*/
public void setStormStart(String stormStart) {
this.stormStart = stormStart;
}
/**
* @return the stormEnd
*/
public String getStormEnd() {
return stormEnd;
}
/**
* @param stormEnd the stormEnd to set
*/
public void setStormEnd(String stormEnd) {
this.stormEnd = stormEnd;
}
/**
* @return the stormMag
*/
public int getStormMag() {
return stormMag;
}
/**
* @param stormMag the stormMag to set
*/
public void setStormMag(int stormMag) {
this.stormMag = stormMag;
}
@Override
public String toString() {
return " " + getStormYear() + ": " + getStormName() + " " +
((getStormMag() == -1) ? "(no info)" :
((getStormMag() == 0) ? "(tropical storm)" : "(hurricane level " +
getStormMag() + ")")) + ": " +
((getStormStart().equals("")) ? "(no start)" : getStormStart().substring(0, 2) + "/"
+ getStormStart().substring(2)) +
" - " +
((getStormEnd().equals("")) ? "(no end)" : getStormEnd().substring(0, 2) + "/" +
getStormEnd().substring(2));
}
}
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Database {
private static final int MAX_SIZE = 50;
//Attributes
private Storm[] stormArr;
private int count;
/**
* Constructor
* Accepts a file and attempts to read it
* Fills the storm array with the data
*/
public Database(File fileName) {
//Initialize array
this.stormArr = new Storm[MAX_SIZE];
this.count = 0;
//Scanner to read from the file
Scanner in = null;
try {
in = new Scanner(fileName);
//Read data from the file
while(in.hasNextLine()) {
//Year of storm/ Name of storm/ mmdd storm started/ mmdd storm ended/ magnitude
of storm
String line = in.nextLine();
String[] data = line.replaceAll("/", "/ ").split("/");
if(data.length < 5)
System.out.println("Database entry not in the correct format: " + line);
//Add data to array
this.stormArr[this.count] = new Storm(data[1].trim(), Integer.parseInt(data[0].trim()),
data[2].trim(), data[3].trim(),
(data[4].trim().equals("")) ? -1 : Integer.parseInt(data[4].trim()));
this.count += 1;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
//Close scanner
if(in == null)
in.close();
}
}
/**
* Returns a storm array which matches the name
*
* @param name
*/
public void getStormsByName(String name) {
boolean found = false;
for (Storm storm : stormArr) {
if((storm != null ) && (storm.getStormName().equalsIgnoreCase(name))) {
found = true;
System.out.print(storm);
}
}
if(!found)
System.out.println("Could not find any storm named "" + name + "".");
}
/**
* Returns a storm array which matches the year
*
* @param year
*/
public void getStormsByYear(int year) {
boolean found = false;
for (Storm storm : stormArr) {
if((storm != null ) && (storm.getStormYear() == year)) {
found = true;
System.out.print(storm);
}
}
if(!found)
System.out.println("Could not find any storm in the year "" + year + "".");
}
/**
* Prints all storm details
*/
public void printAll() {
if(this.stormArr.length == 0)
System.out.println("No data.");
else {
for (Storm storm : stormArr) {
if(storm != null)
System.out.print(storm);
}
}
}
}
import java.io.File;
import java.util.Scanner;
public class Prog1 {
/**
* Displays a list of commands
*/
public static void printCommands() {
System.out.println(" Current available commands:");
System.out.println("1 --> Search for a storm name");
System.out.println("2 --> Search for a year");
System.out.println("3 --> Print all storms");
System.out.println("9 --> Exit");
}
public static void main(String args[]) {
if (args.length == 0) // Check if file name is provided in the command
// line
System.out.println("File name not specified");
else {
File f = new File(args[0]);
if (!f.exists()) // Check if file exists
System.out.println("Input file does not exist.");
else if (f.length() == 0) {
System.out.println("No data in the file");
} else {
// Create Database object
Database db = new Database(f);
// Scanner to get user input
Scanner in = new Scanner(System.in);
// Variable to get user command
int cmd = 0;
//Start
System.out.println("Welcome to the CS-102 Storm Tracker Program");
while (true) {
printCommands();
System.out.print("Your choice? ");
cmd = in.nextInt();
in.nextLine(); //Clear keyboard buffer
switch (cmd) {
case 1: //Search storm by name
System.out.println("Enter storm name prefix: ");
String name = in.nextLine();
db.getStormsByName(name);
break;
case 2: //Search storm by year
System.out.println("Enter storm year: ");
int year = in.nextInt();
db.getStormsByYear(year);
break;
case 3: //Print all storms
db.printAll();
break;
case 9: //Exit
in.close();
System.exit(0);
default:
System.out.println("Invalid command. Please try again.");
}
System.out.println();
}
}
}
}
}
SAMPLE INPUT:
2004/Ali///
2003/Bob///
1980/Sarah/0123/0312/0
1956/Michael/1211/1223/4
1988/Ryan/0926/1019/
1976/Tim/0318/1010/0
2006/Ronald/0919/1012/2
1996/Mona/0707/0723/1
2000/Kim/0101/0201/1
2000/Jim/1101//3
SAMPLE OUTPUT:
Welcome to the CS-102 Storm Tracker Program
Current available commands:
1 --> Search for a storm name
2 --> Search for a year
3 --> Print all storms
9 --> Exit
Your choice? 3
2004: Ali (no info): (no start) - (no end)
2003: Bob (no info): (no start) - (no end)
1980: Sarah (tropical storm): 01/23 - 03/12
1956: Michael (hurricane level 4): 12/11 - 12/23
1988: Ryan (no info): 09/26 - 10/19
1976: Tim (tropical storm): 03/18 - 10/10
2006: Ronald (hurricane level 2): 09/19 - 10/12
1996: Mona (hurricane level 1): 07/07 - 07/23
2000: Kim (hurricane level 1): 01/01 - 02/01
2000: Jim (hurricane level 3): 11/01 - (no end)
Current available commands:
1 --> Search for a storm name
2 --> Search for a year
3 --> Print all storms
9 --> Exit
Your choice? 1
Enter storm name prefix:
Ali
2004: Ali (no info): (no start) - (no end)
Current available commands:
1 --> Search for a storm name
2 --> Search for a year
3 --> Print all storms
9 --> Exit
Your choice? 2
Enter storm year:
2000
2000: Kim (hurricane level 1): 01/01 - 02/01
2000: Jim (hurricane level 3): 11/01 - (no end)
Current available commands:
1 --> Search for a storm name
2 --> Search for a year
3 --> Print all storms
9 --> Exit
Your choice? 9

More Related Content

PDF
JavaI have the full code complete i just need it to be replace th.pdf
PDF
URGENTJavaPlease updated the already existing Java program and m.pdf
PDF
javaFix in the program belowhandle incomplete data for text fil.pdf
PDF
Assignment Details There is a .h file on Moodle that provides a defi.pdf
PDF
RainFall.java public class RainFall {    private double months.pdf
PDF
“According to new research in the Journal of Research in Personality.pdf
PDF
Trueas each officer has fixed paroleelesratio = no of parollesn.pdf
PDF
to upperSolutionto upper.pdf
JavaI have the full code complete i just need it to be replace th.pdf
URGENTJavaPlease updated the already existing Java program and m.pdf
javaFix in the program belowhandle incomplete data for text fil.pdf
Assignment Details There is a .h file on Moodle that provides a defi.pdf
RainFall.java public class RainFall {    private double months.pdf
“According to new research in the Journal of Research in Personality.pdf
Trueas each officer has fixed paroleelesratio = no of parollesn.pdf
to upperSolutionto upper.pdf

More from sharnapiyush773 (20)

PDF
there are several theory on defining acid and base, Lewis acids and .pdf
PDF
The Statement is False. As there are many applications of hyperbo;ic.pdf
PDF
The Sarbanes-Oxley , 2002 contains following provisions which determ.pdf
PDF
The main motivation behind software reuse is to avoid wastage of tim.pdf
PDF
The expression evaluation is compiler dependent, and may vary. A g.pdf
PDF
picture is missingSolutionpicture is missing.pdf
PDF
package employeeType.employee;public class Employee {    private.pdf
PDF
OSI (Open Systems Interconnection) is reference model for how applic.pdf
PDF
Note Modified codecode#includeiostream #include stdio.h.pdf
PDF
Oligotrophic area Usually, an olidgotropic organism is a one that .pdf
PDF
n = N 2^(rt)n = no of people at a given timeN = initial populat.pdf
PDF
Maroochy Shire Sewage Spill Case Study Student’s Name Institution Af.pdf
PDF
It lacks the origin of replication which is most important for the i.pdf
PDF
In developmental biology, an embryo is divided into two hemispheres.pdf
PDF
Bryophytes- Development of primitive vasculature for water transport.pdf
PDF
givenSolutiongiven.pdf
PDF
Ethernet II framing (also known as DIX Ethernet, named after DEC, In.pdf
PDF
content analysis refers to an analytical process for measuring the s.pdf
PDF
Because acetone is soluble in water.Since both are miscible They c.pdf
PDF
AnswerDichlorodiphenyltrichloroethane (DDT) is an organochlorine .pdf
there are several theory on defining acid and base, Lewis acids and .pdf
The Statement is False. As there are many applications of hyperbo;ic.pdf
The Sarbanes-Oxley , 2002 contains following provisions which determ.pdf
The main motivation behind software reuse is to avoid wastage of tim.pdf
The expression evaluation is compiler dependent, and may vary. A g.pdf
picture is missingSolutionpicture is missing.pdf
package employeeType.employee;public class Employee {    private.pdf
OSI (Open Systems Interconnection) is reference model for how applic.pdf
Note Modified codecode#includeiostream #include stdio.h.pdf
Oligotrophic area Usually, an olidgotropic organism is a one that .pdf
n = N 2^(rt)n = no of people at a given timeN = initial populat.pdf
Maroochy Shire Sewage Spill Case Study Student’s Name Institution Af.pdf
It lacks the origin of replication which is most important for the i.pdf
In developmental biology, an embryo is divided into two hemispheres.pdf
Bryophytes- Development of primitive vasculature for water transport.pdf
givenSolutiongiven.pdf
Ethernet II framing (also known as DIX Ethernet, named after DEC, In.pdf
content analysis refers to an analytical process for measuring the s.pdf
Because acetone is soluble in water.Since both are miscible They c.pdf
AnswerDichlorodiphenyltrichloroethane (DDT) is an organochlorine .pdf
Ad

Recently uploaded (20)

PPTX
Cell Types and Its function , kingdom of life
PPTX
Institutional Correction lecture only . . .
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
Computing-Curriculum for Schools in Ghana
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
master seminar digital applications in india
PDF
Basic Mud Logging Guide for educational purpose
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
TR - Agricultural Crops Production NC III.pdf
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
Sports Quiz easy sports quiz sports quiz
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
Cell Types and Its function , kingdom of life
Institutional Correction lecture only . . .
Final Presentation General Medicine 03-08-2024.pptx
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPH.pptx obstetrics and gynecology in nursing
Computing-Curriculum for Schools in Ghana
STATICS OF THE RIGID BODIES Hibbelers.pdf
Supply Chain Operations Speaking Notes -ICLT Program
master seminar digital applications in india
Basic Mud Logging Guide for educational purpose
Microbial diseases, their pathogenesis and prophylaxis
human mycosis Human fungal infections are called human mycosis..pptx
Microbial disease of the cardiovascular and lymphatic systems
TR - Agricultural Crops Production NC III.pdf
Module 4: Burden of Disease Tutorial Slides S2 2025
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Sports Quiz easy sports quiz sports quiz
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Renaissance Architecture: A Journey from Faith to Humanism
Ad

public class Storm {   Attributes    private String stormName;.pdf

  • 1. public class Storm { //Attributes private String stormName; private int stormYear; private String stormStart; private String stormEnd; private int stormMag; /** * Constructor * * @param stormName * @param stormYear * @param stormStart * @param stormEnd * @param stormMag */ public Storm(String stormName, int stormYear, String stormStart, String stormEnd, int stormMag) { this.stormName = stormName; this.stormYear = stormYear; this.stormStart = stormStart; this.stormEnd = stormEnd; this.stormMag = stormMag; } /** * @return the stormName */ public String getStormName() { return stormName; } /** * @param stormName the stormName to set */
  • 2. public void setStormName(String stormName) { this.stormName = stormName; } /** * @return the stormYear */ public int getStormYear() { return stormYear; } /** * @param stormYear the stormYear to set */ public void setStormYear(int stormYear) { this.stormYear = stormYear; } /** * @return the stormStart */ public String getStormStart() { return stormStart; } /** * @param stormStart the stormStart to set */ public void setStormStart(String stormStart) { this.stormStart = stormStart; } /** * @return the stormEnd */ public String getStormEnd() { return stormEnd; } /** * @param stormEnd the stormEnd to set */
  • 3. public void setStormEnd(String stormEnd) { this.stormEnd = stormEnd; } /** * @return the stormMag */ public int getStormMag() { return stormMag; } /** * @param stormMag the stormMag to set */ public void setStormMag(int stormMag) { this.stormMag = stormMag; } @Override public String toString() { return " " + getStormYear() + ": " + getStormName() + " " + ((getStormMag() == -1) ? "(no info)" : ((getStormMag() == 0) ? "(tropical storm)" : "(hurricane level " + getStormMag() + ")")) + ": " + ((getStormStart().equals("")) ? "(no start)" : getStormStart().substring(0, 2) + "/" + getStormStart().substring(2)) + " - " + ((getStormEnd().equals("")) ? "(no end)" : getStormEnd().substring(0, 2) + "/" + getStormEnd().substring(2)); } } import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class Database { private static final int MAX_SIZE = 50; //Attributes private Storm[] stormArr; private int count;
  • 4. /** * Constructor * Accepts a file and attempts to read it * Fills the storm array with the data */ public Database(File fileName) { //Initialize array this.stormArr = new Storm[MAX_SIZE]; this.count = 0; //Scanner to read from the file Scanner in = null; try { in = new Scanner(fileName); //Read data from the file while(in.hasNextLine()) { //Year of storm/ Name of storm/ mmdd storm started/ mmdd storm ended/ magnitude of storm String line = in.nextLine(); String[] data = line.replaceAll("/", "/ ").split("/"); if(data.length < 5) System.out.println("Database entry not in the correct format: " + line); //Add data to array this.stormArr[this.count] = new Storm(data[1].trim(), Integer.parseInt(data[0].trim()), data[2].trim(), data[3].trim(), (data[4].trim().equals("")) ? -1 : Integer.parseInt(data[4].trim())); this.count += 1; } } catch (FileNotFoundException e) { e.printStackTrace(); } finally { //Close scanner if(in == null)
  • 5. in.close(); } } /** * Returns a storm array which matches the name * * @param name */ public void getStormsByName(String name) { boolean found = false; for (Storm storm : stormArr) { if((storm != null ) && (storm.getStormName().equalsIgnoreCase(name))) { found = true; System.out.print(storm); } } if(!found) System.out.println("Could not find any storm named "" + name + ""."); } /** * Returns a storm array which matches the year * * @param year */ public void getStormsByYear(int year) { boolean found = false; for (Storm storm : stormArr) { if((storm != null ) && (storm.getStormYear() == year)) { found = true; System.out.print(storm); } }
  • 6. if(!found) System.out.println("Could not find any storm in the year "" + year + ""."); } /** * Prints all storm details */ public void printAll() { if(this.stormArr.length == 0) System.out.println("No data."); else { for (Storm storm : stormArr) { if(storm != null) System.out.print(storm); } } } } import java.io.File; import java.util.Scanner; public class Prog1 { /** * Displays a list of commands */ public static void printCommands() { System.out.println(" Current available commands:"); System.out.println("1 --> Search for a storm name"); System.out.println("2 --> Search for a year"); System.out.println("3 --> Print all storms"); System.out.println("9 --> Exit"); } public static void main(String args[]) { if (args.length == 0) // Check if file name is provided in the command // line System.out.println("File name not specified"); else {
  • 7. File f = new File(args[0]); if (!f.exists()) // Check if file exists System.out.println("Input file does not exist."); else if (f.length() == 0) { System.out.println("No data in the file"); } else { // Create Database object Database db = new Database(f); // Scanner to get user input Scanner in = new Scanner(System.in); // Variable to get user command int cmd = 0; //Start System.out.println("Welcome to the CS-102 Storm Tracker Program"); while (true) { printCommands(); System.out.print("Your choice? "); cmd = in.nextInt(); in.nextLine(); //Clear keyboard buffer switch (cmd) { case 1: //Search storm by name System.out.println("Enter storm name prefix: "); String name = in.nextLine(); db.getStormsByName(name); break; case 2: //Search storm by year System.out.println("Enter storm year: "); int year = in.nextInt(); db.getStormsByYear(year); break; case 3: //Print all storms db.printAll(); break; case 9: //Exit in.close();
  • 8. System.exit(0); default: System.out.println("Invalid command. Please try again."); } System.out.println(); } } } } } SAMPLE INPUT: 2004/Ali/// 2003/Bob/// 1980/Sarah/0123/0312/0 1956/Michael/1211/1223/4 1988/Ryan/0926/1019/ 1976/Tim/0318/1010/0 2006/Ronald/0919/1012/2 1996/Mona/0707/0723/1 2000/Kim/0101/0201/1 2000/Jim/1101//3 SAMPLE OUTPUT: Welcome to the CS-102 Storm Tracker Program Current available commands: 1 --> Search for a storm name 2 --> Search for a year 3 --> Print all storms 9 --> Exit Your choice? 3 2004: Ali (no info): (no start) - (no end) 2003: Bob (no info): (no start) - (no end) 1980: Sarah (tropical storm): 01/23 - 03/12 1956: Michael (hurricane level 4): 12/11 - 12/23 1988: Ryan (no info): 09/26 - 10/19 1976: Tim (tropical storm): 03/18 - 10/10 2006: Ronald (hurricane level 2): 09/19 - 10/12
  • 9. 1996: Mona (hurricane level 1): 07/07 - 07/23 2000: Kim (hurricane level 1): 01/01 - 02/01 2000: Jim (hurricane level 3): 11/01 - (no end) Current available commands: 1 --> Search for a storm name 2 --> Search for a year 3 --> Print all storms 9 --> Exit Your choice? 1 Enter storm name prefix: Ali 2004: Ali (no info): (no start) - (no end) Current available commands: 1 --> Search for a storm name 2 --> Search for a year 3 --> Print all storms 9 --> Exit Your choice? 2 Enter storm year: 2000 2000: Kim (hurricane level 1): 01/01 - 02/01 2000: Jim (hurricane level 3): 11/01 - (no end) Current available commands: 1 --> Search for a storm name 2 --> Search for a year 3 --> Print all storms 9 --> Exit Your choice? 9 Solution public class Storm { //Attributes private String stormName; private int stormYear; private String stormStart;
  • 10. private String stormEnd; private int stormMag; /** * Constructor * * @param stormName * @param stormYear * @param stormStart * @param stormEnd * @param stormMag */ public Storm(String stormName, int stormYear, String stormStart, String stormEnd, int stormMag) { this.stormName = stormName; this.stormYear = stormYear; this.stormStart = stormStart; this.stormEnd = stormEnd; this.stormMag = stormMag; } /** * @return the stormName */ public String getStormName() { return stormName; } /** * @param stormName the stormName to set */ public void setStormName(String stormName) { this.stormName = stormName; } /** * @return the stormYear */
  • 11. public int getStormYear() { return stormYear; } /** * @param stormYear the stormYear to set */ public void setStormYear(int stormYear) { this.stormYear = stormYear; } /** * @return the stormStart */ public String getStormStart() { return stormStart; } /** * @param stormStart the stormStart to set */ public void setStormStart(String stormStart) { this.stormStart = stormStart; } /** * @return the stormEnd */ public String getStormEnd() { return stormEnd; } /** * @param stormEnd the stormEnd to set */ public void setStormEnd(String stormEnd) { this.stormEnd = stormEnd; } /** * @return the stormMag */
  • 12. public int getStormMag() { return stormMag; } /** * @param stormMag the stormMag to set */ public void setStormMag(int stormMag) { this.stormMag = stormMag; } @Override public String toString() { return " " + getStormYear() + ": " + getStormName() + " " + ((getStormMag() == -1) ? "(no info)" : ((getStormMag() == 0) ? "(tropical storm)" : "(hurricane level " + getStormMag() + ")")) + ": " + ((getStormStart().equals("")) ? "(no start)" : getStormStart().substring(0, 2) + "/" + getStormStart().substring(2)) + " - " + ((getStormEnd().equals("")) ? "(no end)" : getStormEnd().substring(0, 2) + "/" + getStormEnd().substring(2)); } } import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class Database { private static final int MAX_SIZE = 50; //Attributes private Storm[] stormArr; private int count; /** * Constructor * Accepts a file and attempts to read it * Fills the storm array with the data */
  • 13. public Database(File fileName) { //Initialize array this.stormArr = new Storm[MAX_SIZE]; this.count = 0; //Scanner to read from the file Scanner in = null; try { in = new Scanner(fileName); //Read data from the file while(in.hasNextLine()) { //Year of storm/ Name of storm/ mmdd storm started/ mmdd storm ended/ magnitude of storm String line = in.nextLine(); String[] data = line.replaceAll("/", "/ ").split("/"); if(data.length < 5) System.out.println("Database entry not in the correct format: " + line); //Add data to array this.stormArr[this.count] = new Storm(data[1].trim(), Integer.parseInt(data[0].trim()), data[2].trim(), data[3].trim(), (data[4].trim().equals("")) ? -1 : Integer.parseInt(data[4].trim())); this.count += 1; } } catch (FileNotFoundException e) { e.printStackTrace(); } finally { //Close scanner if(in == null) in.close(); } } /** * Returns a storm array which matches the name
  • 14. * * @param name */ public void getStormsByName(String name) { boolean found = false; for (Storm storm : stormArr) { if((storm != null ) && (storm.getStormName().equalsIgnoreCase(name))) { found = true; System.out.print(storm); } } if(!found) System.out.println("Could not find any storm named "" + name + ""."); } /** * Returns a storm array which matches the year * * @param year */ public void getStormsByYear(int year) { boolean found = false; for (Storm storm : stormArr) { if((storm != null ) && (storm.getStormYear() == year)) { found = true; System.out.print(storm); } } if(!found) System.out.println("Could not find any storm in the year "" + year + ""."); } /** * Prints all storm details
  • 15. */ public void printAll() { if(this.stormArr.length == 0) System.out.println("No data."); else { for (Storm storm : stormArr) { if(storm != null) System.out.print(storm); } } } } import java.io.File; import java.util.Scanner; public class Prog1 { /** * Displays a list of commands */ public static void printCommands() { System.out.println(" Current available commands:"); System.out.println("1 --> Search for a storm name"); System.out.println("2 --> Search for a year"); System.out.println("3 --> Print all storms"); System.out.println("9 --> Exit"); } public static void main(String args[]) { if (args.length == 0) // Check if file name is provided in the command // line System.out.println("File name not specified"); else { File f = new File(args[0]); if (!f.exists()) // Check if file exists System.out.println("Input file does not exist."); else if (f.length() == 0) { System.out.println("No data in the file"); } else {
  • 16. // Create Database object Database db = new Database(f); // Scanner to get user input Scanner in = new Scanner(System.in); // Variable to get user command int cmd = 0; //Start System.out.println("Welcome to the CS-102 Storm Tracker Program"); while (true) { printCommands(); System.out.print("Your choice? "); cmd = in.nextInt(); in.nextLine(); //Clear keyboard buffer switch (cmd) { case 1: //Search storm by name System.out.println("Enter storm name prefix: "); String name = in.nextLine(); db.getStormsByName(name); break; case 2: //Search storm by year System.out.println("Enter storm year: "); int year = in.nextInt(); db.getStormsByYear(year); break; case 3: //Print all storms db.printAll(); break; case 9: //Exit in.close(); System.exit(0); default: System.out.println("Invalid command. Please try again."); } System.out.println(); }
  • 17. } } } } SAMPLE INPUT: 2004/Ali/// 2003/Bob/// 1980/Sarah/0123/0312/0 1956/Michael/1211/1223/4 1988/Ryan/0926/1019/ 1976/Tim/0318/1010/0 2006/Ronald/0919/1012/2 1996/Mona/0707/0723/1 2000/Kim/0101/0201/1 2000/Jim/1101//3 SAMPLE OUTPUT: Welcome to the CS-102 Storm Tracker Program Current available commands: 1 --> Search for a storm name 2 --> Search for a year 3 --> Print all storms 9 --> Exit Your choice? 3 2004: Ali (no info): (no start) - (no end) 2003: Bob (no info): (no start) - (no end) 1980: Sarah (tropical storm): 01/23 - 03/12 1956: Michael (hurricane level 4): 12/11 - 12/23 1988: Ryan (no info): 09/26 - 10/19 1976: Tim (tropical storm): 03/18 - 10/10 2006: Ronald (hurricane level 2): 09/19 - 10/12 1996: Mona (hurricane level 1): 07/07 - 07/23 2000: Kim (hurricane level 1): 01/01 - 02/01 2000: Jim (hurricane level 3): 11/01 - (no end) Current available commands: 1 --> Search for a storm name 2 --> Search for a year
  • 18. 3 --> Print all storms 9 --> Exit Your choice? 1 Enter storm name prefix: Ali 2004: Ali (no info): (no start) - (no end) Current available commands: 1 --> Search for a storm name 2 --> Search for a year 3 --> Print all storms 9 --> Exit Your choice? 2 Enter storm year: 2000 2000: Kim (hurricane level 1): 01/01 - 02/01 2000: Jim (hurricane level 3): 11/01 - (no end) Current available commands: 1 --> Search for a storm name 2 --> Search for a year 3 --> Print all storms 9 --> Exit Your choice? 9