URGENT Java Please updated the already existing Java program and modify it with
ID: 3928280 • Letter: U
Question
URGENT
Java
Please updated the already existing Java program and modify it with the steps below:
Where it says ("text file path gets input into here"); in the program link the path instead of that with a text file with this data in it:
Copy and paste this and use as text file and input the path for the text file where it says "text file path gets input into here".
2000/Alex/0110/0120/0
2001/Bill/0210/0220/0
2002/Chris/0310/0320/0
2003/Devon/0140/0420/0
2004/Eli/0510/0520/1
2005/Fred/0610/0620/2
2006/Gilbert/0710/0820/3
2007/Herbert/0910/0920/4
2008/Kim/1010/1020/5
###################################################################
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
//setting variables to string or int
public class Storm {
private int stormYear;
private int stormMag;
private String stormStart;
private String stormEnd;
private String stormName;
/**
* Constructor
* Storing all variables from text file
* @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;
}
/**************************************************************/
/*Method: Get and Set */
/*Purpose: They serve to set&get values from class variables */
/*Parameters: */
/*String target: Storm Name */
/*Return: Storm Name */
/**************************************************************/
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;
}
/**************************************************************/
/*Method:String toString */
/*Purpose: convert to a string */
/*Parameters: */
/*String target: Storm Name */
/*Return: Storm Name */
/**************************************************************/
public String toString() {
String s = " " + getStormYear() + "/ " + getStormName() + " " ;
if(getStormMag() == -1){
s= s + "(no info)";
}
else {
if((getStormMag() == 0)){
s = s + "(tropical storm)";
}
else{
s = s + "(hurricane level " + getStormMag() + ")";
}
if(getStormStart().equals("")){
s = s + "(no start)";
}
else{
s = s + getStormEnd().substring(0, 2) + "/" + getStormEnd().substring(2)+" - " ;
}
if(getStormEnd().equals("")){
s = s + "(no end)" ;
}
else{
s = s + getStormEnd().substring(0, 2) + "/" + getStormEnd().substring(2);
}
}
return s;
}
}
class Database {
private static final int maxarraysize = 50;
//Attributes
private Storm[] stormArr;
private int add;
/**
* 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[maxarraysize];
this.add = 0;
//Scanner to read from the file
Scanner scan = new Scanner(System.in);
try {
scan = new Scanner (new File("text file path gets input into here"));
//Read data from the file
while(scan.hasNextLine()) {
//Year of storm/ Name of storm/ mmdd storm started/ mmdd storm ended/ magnitude of storm
String line = scan.nextLine();
String[] stormdata = line.split("/");
if(stormdata.length < 5)
System.out.println("Database entry not in the correct format: " + line);
//Add data to array
this.stormArr[this.add] = new Storm (stormdata[1], Integer.parseInt(stormdata[0]),stormdata[2], stormdata[3], Integer.parseInt(stormdata[4]));
this.add += 1;
}
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
}
/**
* 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(""" + name + "" was not found as a storm 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(""" + year + "" was not found as a storm 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);
}
}
}
}
class Prog1 {
/**
* Displays a list of commands for user
*/
public static void printCommands() {
System.out.println("Welcome to the CS-102 Storm Tracker Program ");
System.out.println("Current available commands: ");
System.out.println("1. Search for a storm name");
System.out.println("2. Search for a storm year");
System.out.println("3. Print all storms");
System.out.println("4. Exit");
}
public static void main(String args[]) {
File file = new File("text file path gets input into here");
if (!file.exists()) // Check if file is there
System.out.println("file does not exist.");
else {
// Create Database object
Database db = new Database(file);
// Scanner to get user input
Scanner scan = new Scanner(System.in);
// Variable to get user command
int cmd = 0;
//Start
while (true) {
printCommands();
System.out.print("Your choice? ");
cmd = scan.nextInt();
scan.nextLine();
//look up storm name
if (cmd == 1){
System.out.println("Print out storm name: ");
String name = scan.nextLine();
db.getStormsByName(name);
}
//look up storm year
else if (cmd == 2){
System.out.println("Print out storm year: ");
int year = scan.nextInt();
db.getStormsByYear(year);
}
//prints out the data
else if (cmd == 3){
db.printAll();
}
//shuts down program
else if (cmd == 4){
scan.close();
System.exit(0);
}
else {
System.out.println("Please select command 1-4");
}
System.out.println();
}
}
}
}
Explanation / Answer
//Tested on Ubuntu,Linux
//I did changes on Database.java and Prog1.java files
/******************Storm.java*************************/
public class Storm {
private int stormYear;
private int stormMag;
private String stormStart;
private String stormEnd;
private String stormName;
/**
* Constructor
* Storing all variables from text file
* @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;
}
/**************************************************************/
/*Method: Get and Set */
/*Purpose: They serve to set&get values from class variables */
/*Parameters: */
/*String target: Storm Name */
/*Return: Storm Name */
/**************************************************************/
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;
}
/**************************************************************/
/*Method:String toString */
/*Purpose: convert to a string */
/*Parameters: */
/*String target: Storm Name */
/*Return: Storm Name */
/**************************************************************/
public String toString() {
String s = " " + getStormYear() + "/ " + getStormName() + " " ;
if(getStormMag() == -1){
s= s + "(no info)";
}
else {
if((getStormMag() == 0)){
s = s + "(tropical storm)";
}
else{
s = s + "(hurricane level " + getStormMag() + ")";
}
if(getStormStart().equals("")){
s = s + "(no start)";
}
else{
s = s + getStormEnd().substring(0, 2) + "/" + getStormEnd().substring(2)+" - " ;
}
if(getStormEnd().equals("")){
s = s + "(no end)" ;
}
else{
s = s + getStormEnd().substring(0, 2) + "/" + getStormEnd().substring(2);
}
}
return s;
}
}
/************************Database.java*******************/
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Database {
private static final int maxarraysize = 50;
// Attributes
private Storm[] stormArr;
private int add;
/**
* 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[maxarraysize];
this.add = 0;
// Scanner to read from the file
Scanner scan = new Scanner(System.in);
try {
scan = new Scanner(fileName);
// Read data from the file
while (scan.hasNextLine()) {
// Year of storm/ Name of storm/ mmdd storm started/ mmdd storm
// ended/ magnitude of storm
String line = scan.nextLine();
String[] stormdata = line.split("/");
if (stormdata.length < 5)
System.out.println("Database entry not in the correct format: " + line);
// Add data to array
this.stormArr[this.add] = new Storm(stormdata[1], Integer.parseInt(stormdata[0]), stormdata[2],
stormdata[3], Integer.parseInt(stormdata[4]));
this.add += 1;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
/**
* 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(""" + name + "" was not found as a storm 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(""" + year + "" was not found as a storm 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);
}
}
}
}
/*************************Prog1.java***************/
import java.io.File;
import java.util.Scanner;
public class Prog1 {
/**
* Displays a list of commands for user
*/
public static void printCommands() {
System.out.println("Welcome to the CS-102 Storm Tracker Program ");
System.out.println("Current available commands: ");
System.out.println("1. Search for a storm name");
System.out.println("2. Search for a storm year");
System.out.println("3. Print all storms");
System.out.println("4. Exit");
}
public static void main(String args[]) {
File file = new File("/home/anshu/Desktop/chegg/input.txt");//ubuntu file path
if (!file.exists()) // Check if file is there
System.out.println("file does not exist.");
else {
// Create Database object
Database db = new Database(file);
// Scanner to get user input
Scanner scan = new Scanner(System.in);
// Variable to get user command
int cmd = 0;
// Start
while (true) {
printCommands();
System.out.print("Your choice? ");
cmd = scan.nextInt();
scan.nextLine();
// look up storm name
if (cmd == 1) {
System.out.println("Print out storm name: ");
String name = scan.nextLine();
db.getStormsByName(name);
}
// look up storm year
else if (cmd == 2) {
System.out.println("Print out storm year: ");
int year = scan.nextInt();
db.getStormsByYear(year);
}
// prints out the data
else if (cmd == 3) {
db.printAll();
}
// shuts down program
else if (cmd == 4) {
scan.close();
System.exit(0);
} else {
System.out.println("Please select command 1-4");
}
System.out.println();
}
}
}
}
/****************************Output**************************/
Welcome to the CS-102 Storm Tracker Program
Current available commands:
1. Search for a storm name
2. Search for a storm year
3. Print all storms
4. Exit
Your choice? 1
Print out storm name:
Alex
2000/ Alex (tropical storm)01/20 - 01/20
Welcome to the CS-102 Storm Tracker Program
Current available commands:
1. Search for a storm name
2. Search for a storm year
3. Print all storms
4. Exit
Your choice? 2
Print out storm year:
2003
2003/ Devon (tropical storm)04/20 - 04/20
Welcome to the CS-102 Storm Tracker Program
Current available commands:
1. Search for a storm name
2. Search for a storm year
3. Print all storms
4. Exit
Your choice? 3
2000/ Alex (tropical storm)01/20 - 01/20
2001/ Bill (tropical storm)02/20 - 02/20
2002/ Chris (tropical storm)03/20 - 03/20
2003/ Devon (tropical storm)04/20 - 04/20
2004/ Eli (hurricane level 1)05/20 - 05/20
2005/ Fred (hurricane level 2)06/20 - 06/20
2006/ Gilbert (hurricane level 3)08/20 - 08/20
2007/ Herbert (hurricane level 4)09/20 - 09/20
2008/ Kim (hurricane level 5)10/20 - 10/20
Welcome to the CS-102 Storm Tracker Program
Current available commands:
1. Search for a storm name
2. Search for a storm year
3. Print all storms
4. Exit
Your choice? 4
/********************content of input.txt******************/
2000/Alex/0110/0120/0
2001/Bill/0210/0220/0
2002/Chris/0310/0320/0
2003/Devon/0140/0420/0
2004/Eli/0510/0520/1
2005/Fred/0610/0620/2
2006/Gilbert/0710/0820/3
2007/Herbert/0910/0920/4
2008/Kim/1010/1020/5
Thanks a lot
If you have any query please feel free and ask
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.