Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6580817
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:04:28+00:00 2026-05-25T16:04:28+00:00

I wrote a Course class that has constructors that read from text and binary

  • 0

I wrote a Course class that has constructors that read from text and binary files and has methods to write to text and binary files. How do I write a junit test to test this class?

see code below:

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.PrintWriter;
import java.util.Scanner;

public class Course {
//instance variables
private String courseID;
private String courseName;
private int numberOfCredits;
private String departmentID;

public Course(String courseID,String courseName, int numberOfCredits, String departmentID){
    //constructor
    this.courseID=courseID;
    this.courseName=courseName;
    this.numberOfCredits=numberOfCredits;
    this.departmentID=departmentID;
}

public Course(Scanner inputFile)throws Exception{
    //constructor, read data from text file
    try{
        courseID=inputFile.nextLine();
        courseName=inputFile.next();
        numberOfCredits=inputFile.nextInt();
        departmentID=inputFile.next();
    }
    catch(Exception e){
        throw e;
    }

}

public Course (DataInputStream binFile)throws Exception{
    //constructor reads from binary file and assign values to variables
    try{
    courseID=binFile.readUTF();
    courseName=binFile.readUTF();
    numberOfCredits=binFile.readInt();
    departmentID=binFile.readUTF();
    }
    catch(Exception e){
        throw e;
    }
}
public void saveToTextFile(PrintWriter file){
    //prints to text file
    file.printf(" %s %s %d %s ", courseID, courseName, numberOfCredits, departmentID);
}
public void saveToBin(DataOutputStream binFile)throws Exception{
    //saves information to binary file
    binFile.writeUTF(courseID);
    binFile.writeUTF(courseName);
    binFile.writeInt(numberOfCredits);
    binFile.writeUTF(departmentID);
}



public String toString(){
    //setup string for course display
    String info=courseID + " " + courseName+ " "+ numberOfCredits+" "+ departmentID;
    return info;
}

//getters and setters
public String getCourseName() {
    return courseName;
}

public void setCourseName(String courseName) {
    this.courseName = courseName;
}

public int getNumberOfCredits() {
    return numberOfCredits;
}

public void setNumberOfCredits(int numberOfCredits) {
    this.numberOfCredits = numberOfCredits;
}

public String getDepartmentID() {
    return departmentID;
}

public void setDepartmentID(String departmentID) {
    this.departmentID = departmentID;
}

public String getCourseID() {
    return courseID;
}

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T16:04:29+00:00Added an answer on May 25, 2026 at 4:04 pm

    When you’re testing constructors you typically only need the following:

    • Ensure that the object created contains the values you expect (i.e. the values you passed in, or the data that results from some computation done on them)
    • The data loaded into the objects matches those in the file (or another InputStream, if you don’t want to use actual files in your tests)
    • Exceptions are thrown for invalid input

    Other than that, there isn’t much to do for constructors. In the case of your first constructor, which simply assigns values (without doing any disk I/O) I don’t tend to test those constructors, because all you’re really verifying is that the = operator in Java works, which we know is does.

    So, I would only write tests that cover the above situations for your second constructor.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote a windows service using VB that read some legacy data from Visual
I have a class TChild derived from TParent. TParent has a property MyProp that
My view model MyViewModel has a property MyProperty that i need to read/save from/to
I'm trying to write a small class library for a C++ course. I was
I wrote a component that displays a filename, a thumbnail and has a button
I created a class derived from TThread that executes in background a query. I
How can I enforce that my test class covers a particular interface? Also is
I'm a TA for an Introduction to MATLAB course, and the class has not
I wrote a program to play MPEG video on a window (of course, DirectShow
I have to write a mashup application for an university course and I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.