Search code examples
javaconstructorencapsulation

Constructor error with encapsulation


I have my code in 3 different files using encapsulation (Data hiding) and i have 1 problem at the very end of my code in my if and else statement (very bottom) when trying to call the classes from the other 2 documents. I will put the code in 1st document to 3rd document. Any suggestions on what I did wrong?

// FIRST DOCUMENT
public class CollegeCourse { //class name

//variables
String deptName;
int courseNum;
int credits = 3;
double fee;

  //constructor
  public CollegeCourse(String department, int course, int Credits) {

     deptName = department;
     courseNum = course;
     credits = Credits;
     fee = credits * 120;
  }

  //getters setters
  public String getdepartment() {
     return deptName;      
  }
  public String setdepartment(String dept) {
     return dept = deptName;      
  }
  public int getcourse() {
     return courseNum;      
  }
  public int setcourse(int c) {
     return c = courseNum;      
  }
  public int getCredits() {
     return credits;      
  }      
  public int setCredits(int cred) {
     return cred = credits;      
  }

  public void display()
  {
  System.out.println("Department: " + deptName);
  System.out.println("Course Number: " + courseNum);
  System.out.println("Credits: " + credits);
  System.out.println("Fee: $" + fee);
  }
}
//SECOND DOCUMENT
public class LabCourse extends CollegeCourse { //polymorphism extending         CollegeCourse class into LabCourse class. 

//constructor
public LabCourse(String department, int course, int Credits){
//add 50 dollars to the fee 
  super(department, course, Credits);
  fee = fee + 50;
}
//display the course
  public void display(){
  System.out.print("This course is a lab course" + fee);
  System.out.println("Department: " + deptName);
  System.out.println("Course Number: " + courseNum);
  System.out.println("Credits: " + credits);
  System.out.println("Fee: $" + fee);
  }
}    
//THIRD DOCUMENT MAIN HEADER
import java.util.Scanner;
public class UseCourse {

public static void main(String[] args){ 

  String s, c, cd;

  Scanner input = new Scanner(System.in);
  System.out.print("Enter: BIO, CHEM, ENG, MATH: ");
  s = input.nextLine();   

  System.out.print("What is the course number: ");
  c = input.nextLine();      

  System.out.print("How many credits: ");
  cd = input.nextLine();      

  if(s.equals ("BIO") || s.equals ("CHEM")){
     LabCourse lc = new LabCourse(department, course, Credits); //here is my problem, it  can't find the CollegeCourse class department, course,//and credits...
     lc.display();       
  }

  else {
     CollegeCourse cc = new CollegeCourse(department, course, Credits); //here is my problem, it  can't find the CollegeCourse class department, course,//and credits...
     cc.display();
  }

   }  
 }

Here is the error that i'm getting.

UseCourse.java:24: error: cannot find symbol
     LabCourse lc = new LabCourse(department, course, Credits);
                                  ^

And it repeats for each error "department, course, Credits"

UseCourse.java:29: error: cannot find symbol
     CollegeCourse cc = new CollegeCourse(department, course, Credits);
                                                      ^

Solution

  • Your parameters in your constructor call are all wrong. Neither department, course or Credits are defined, so you would need to use s, c and cd instead, as those are the variables you're using for your input.

    Furthermore, you need to read c and cd as integers and pass those to your constructor as follows:

    System.out.print("What is the course number: ");
    int c = input.nextInt();
    
    System.out.print("How many credits: ");
    int cd = input.nextInt();
    
    // ...
    
    LabCourse lc = new LabCourse(s, c, cd);