Change this program so that a user can choose a specific season, and time period
ID: 3626588 • Letter: C
Question
Change this program so that a user can choose a specific season, and time period, and have that specific traffic level displayed/**
*
*/
class Testing {
public static void main(String[] args){
int seasons[] ={5,3,1,1};//Seasons will go winter, spring, summer, fall in array 0,1,2,3
int times[] = {5,2,4,1,5,2,2}; //6-10, 9-11, noon, 12-3, 3-6, 6-9, 9-12am
int traffic;
String trafficname="";
String name="";
String time="";
int i=0, k=0;
for (i=0; i<seasons.length; i++){
for ( k=0; k<times.length; k++){
switch(i+1){
case 1: {
name = "Winter";
break;
}
case 2: {
name = "Spring";
break;
}
case 3: {
name = "Summer";
break;
}
case 4: {
name = "Fall";
break;
}
default:{
//null
}
}//End first switch
switch (k+1){
case 1: {
time = "6-10am";
break;
}
case 2: {
time = "9-11am";
break;
}
case 3: {
time = "Noon";
break;
}
case 4: {
time = "12-3pm";
break;
}
case 5: {
time = "3-6pm";
break;
}
case 6: {
time = "6-9pm";
break;
}
case 7: {
time = "9pm-12am";
break;
}
default:{
//null
}
}//End second switch
traffic = seasons[i]+times[k];
if (traffic >0 && traffic <=4){
trafficname = "low";
}
else if (traffic >4 && traffic <=7){
trafficname = "Medium";
}
else if (traffic >7 && traffic <=10){
trafficname = "High";
}
System.out.println("The traffic volume for the season: "+name+ " at: "+ time+ " Is: "+ trafficname);
}//End inner for
}//end outer for
}//end main
}//end class
Explanation / Answer
import java.util.*;
class Testing {
public static final int seasons[] ={5,3,1,1};//Seasons will go winter, spring, summer, fall in array 0,1,2,3
public static final int times[] = {5,2,4,1,5,2,2}; //6-10, 9-11, noon, 12-3, 3-6, 6-9, 9-12am
public static final String[] season_names = {"Winter", "Spring", "Summer", "Fall"};
public static final String[] time_names = {"6-10am", "9-11am", "Noon", "12-3pm", "3-6pm", "6-9pm", "9pm-12am"};
public static void main(String[] args)
{
int traffic;
String trafficname="";
String name="";
String time="";
int i=0, k=0;
// keyboard input
Scanner kb = new Scanner(System.in);
System.out.print("Enter the season: ");
name = kb.next(); // example input: "Winter"
System.out.print("Enter the time: ");
time = kb.next(); // example input: 6-10am
// set i and k
for( ; i < season_names.length; i++)
if(season_names[i].equals(name))
break;
if(i >= season_names.length)
{
System.out.print("Invalid season");
return;
}
for( ; k < time_names.length; k++)
if(time_names[k].equals(time))
break;
if(k >= time_names.length)
{
System.out.println("Invalid time");
return;
}
// determine traffic
traffic = seasons[i]+times[k];
if (traffic >0 && traffic <=4){
trafficname = "low";
}
else if (traffic >4 && traffic <=7){
trafficname = "Medium";
}
else if (traffic >7 && traffic <=10){
trafficname = "High";
}
System.out.println("The traffic volume for the season: "+name+ " at: "+ time+ " is "+ trafficname);
}//end main
}//end class
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.