Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program in JAVA Motivation: to practice exceptions. Assignment. Define a

ID: 3569087 • Letter: W

Question

Write a program in JAVA

Motivation: to practice exceptions. Assignment. Define a class Course. The description of each course should contain: a class title. the corresponding major, and the number of students taking this course. This class should contains: a constructor method and get-and set-methods. Make the constructor method throw an IllegalArgunentException exception if someone tries to input a negative number of students. Catch the exceptions thrown by this method and print appropriate error messages. Write a method that ask the user for the number of courses and then forms an array of courses, by asking the user all this information about the course. Write a second method that, given an array of courses, prints this information course by course.

Explanation / Answer

Here you go :)

Comment if u have any doubts.

//Course class

import java.io.*;

public class Course
{
   private String title,major;
   private int no;

   public Course(String title,String major, int no) throws IllegalArgumentException
   {
       if(no<0)throw new IllegalArgumentException();
       else
       {
           this.title=title;
           this.major=major;
           this.no=no;
       }
   }

   public String getTitle() {
       return title;
   }

   public String getMajor() {
       return major;
   }

   public int getNo() {
       return no;
   }

   public void setTitle(String title) {
       this.title = title;
   }

   public void setMajor(String major) {
       this.major = major;
   }

   public void setNo(int no) {
       this.no = no;
   }
  
   public static Course[] getCourses() throws IOException
   {
       BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
       System.out.println("No of courses: ");
       int nos=Integer.parseInt(br.readLine());
      
       Course[] cos=new Course[nos];
       for(int i=0;i<nos;i++)
       {
           System.out.println("Enter title of "+(i+1)+" course: ");
           String t=br.readLine();
           System.out.println("Enter major of "+(i+1)+" course: ");
           String m=br.readLine();
           System.out.println("Enter no.of students in "+(i+1)+" course: ");
           int n=Integer.parseInt(br.readLine());
          
           cos[i]=new Course(t,m,n);
       }
       return cos;
   }
  
   public static void printCourses(Course[] co)
   {
       for(int i=0;i<co.length;i++)
       {
           System.out.println("Course title: "+co[i].getTitle()+" Course major: "+co[i].getMajor()+" No.of Students in course: "+co[i].getNo());
       }
   }
  
   public static void main(String[] args)
   {

       try{
           Course c1=new Course("Course 1","Math",12);
           System.out.println("Course title: "+c1.getTitle()+" Course major: "+c1.getMajor()+" No.of Students in course: "+c1.getNo());
          
           Course[] c2=Course.getCourses();
           Course.printCourses(c2);
       }
       catch(IllegalArgumentException e2){System.out.println("Enter valid no of students!!");}
       catch(IOException e1){System.out.println("Enter valid input!!");}
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote