Assignment: Write a Java program that stores at least four different course name
ID: 3795999 • Letter: A
Question
Assignment:
Write a Java program that stores at least four different course names and meeting days and times in a two-dimensional array. Allow the user to enter a course name (such as “COP 2800”), and display the day of the week and time that the course is held (such as “Wed 10:00am”). If the course does not exist, display an error message. Save the file as Course.java
Purpose:
Able to create and populate a two-dimensional array
Able to use a for loop Able to use if statements
Able to interrogate strings for a specific characters.
Use the print statement for output
Explanation / Answer
Hi,
Please see the java class and sample output. Please comment for any queries/feedbacks.
thanks,
Anita
Course.java
import java.util.Scanner;
public class Course {
public static void main(String [] args){
String courseName="";
String time = "";
boolean courseFound =false;
//Creating course details array and Storing values in Course Details Array
String[][] courseDetailsArray = {{"COP 2800","Wed 10:00am"},{"COP 2801","Thur 10:00am"},{"COP 2802","Fri 10:00am"},{"COP 2803","Mon 10:00am"}};
//Reading the course name from user
Scanner scan = new Scanner(System.in);
System.out.println("Please enter a Course Name :");
courseName = scan.nextLine();
//looping through each row
for(int i=0;i<4;i++){
//looping through each column
for(int j=0;j<2;j++){
String arrayElement = courseDetailsArray[i][j];
if(courseName.equalsIgnoreCase(arrayElement)){
//if the Coursename matche with courseDetailsArray[i][j]
//get the next element of the same row as time
time = courseDetailsArray[i][j+1];
courseFound = true; //Setting the flag for row loop
break; // break the iteration
}
}
if(courseFound){
break;
}
}
//Printing the details
if(courseFound){
System.out.println("Course Time is : "+time);
}
else{
System.out.println("Course not found");
}
}
}
Sample output:
Please enter a Course Name :
COP 2800
Course Time is : Wed 10:00am
Please enter a Course Name :
COP 2804
Course not found
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.