Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

How to write Junit tests for Java code below in Java ? How do you write JUnit tests for Part 4 ? I have included

How to write Junit tests for Java code below in Java ?

How do you write JUnit tests for Part 4 ? I have included the code below

I have part 1 , finished

part 2, maybe correct

part 3 correct

part 4

I have included my code below:

Class 3 Chapter 17 Lab( Store objects in a file)

Due Wednesday by 6pm Points 10 Submitting a text entry box, a website url, or a file upload Available Feb 8 at 6pm - Feb 15 at 6pm

Program 3.2 Requirements

Part 1: Maven

Generate a maven project and import it into Eclipse like we did in class. Use the java11-junit5 archetype, the group should be com.jccc.objects.file and the name of the project is objects-file. Take a screenshot of eclipse showing the included project. The project should be expanded, so the folders can be seen. upload the pom.xml and screenshot as part of the this lab.

Part 2 -- create an object

DVDs. The DVDs have 3 attributes: name -- 32 bytes length of film -- 5 bytes MPAA rating -- 2 bytes

Part 3 -- Write a program

The main method of the program should just call the 4 methods below Create a method that creates a List of 5 DVD. The DVD can be whatever you like but must contain all 3 attributes. Write a method Using Object Streams that stores the List of DVDs(use the method above). a Date object for the current time, and the double value 5.5 into a file named class03Object.dat. Write a method Using Object Streams that reads the data from class03Object.dat. The method returns a List Write a method using RandomAccessFile that stores the List of DVDs(use the method above) into a file named class03RAF.dat. Write a method Using RandomAccessFile that reads the data from class03RAF.dat. The method returns a List

Part 4 -- Write Junit tests --Reminder Test files should have the same name as the file that they are testing plus the ending Test

write a Junit test method that calls the write method Using Object Streams and then call the read method and assert the values read. write a second Junit test method that calls the write method Using RandomAccessFile and then call the read method and assert the values read.

Expected Documentation

JavaDoc with @author and your name and a brief description of the class. Also, each method should have Javadoc on it. Checkstyle must be used. Not using Checkstyle will be an automatic loss of 10%.

Submission

Upload the files for each part to this lab submission page. There should be 5 files.

DVD.java

package com.jccc.objects.file;

public class DVD {

private String name; private String lengthOfFilm; private String rating;

public DVD(String name, String lengthOfFilm, String rating) { super(); this.name = name; this.lengthOfFilm = lengthOfFilm; this.rating = rating; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getLengthOfFilm() { return lengthOfFilm; } public void setLengthOfFilm(String lengthOfFilm) { this.lengthOfFilm = lengthOfFilm; } public String getRating() { return rating; } public void setRating(String rating) { this.rating = rating; }

}

package com.jccc.objects.file;

import java.io.*; import java.util.ArrayList; import java.util.Date; import java.util.List;

public class mainDvdFile {

public static void main(String[] args) throws IOException, ClassNotFoundException { List dvds = createDVDList(); writeToObjFile(dvds); List objList = readFromObjFile(); writeToRAFFile(dvds); List rafList = readFromRAFFile(); }

/** * Creates a list of 5 DVD objects */ private static List createDVDList() { List dvdList = new ArrayList<>(); dvdList.add(new DVD("Kung Fu Panda", "65", "PG")); dvdList.add(new DVD("Karate Kid", "90", "PG-13")); dvdList.add(new DVD("Ice Age", "85", "PG")); dvdList.add(new DVD("Titanic", "89", "R")); dvdList.add(new DVD("Spiderman", "75", "R")); return dvdList; }

/** * Writes the list of DVDs, a Date object, and a double value to an object file */ private static void writeToObjFile(List dvds) throws IOException { try (ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream("class03Object.dat"))) { output.writeObject(dvds); output.writeObject(new Date()); output.writeDouble(5.5); } }

/** * Reads the list of DVDs, a Date object, and a double value from an object file */ private static List readFromObjFile() throws IOException, ClassNotFoundException { try (ObjectInputStream input = new ObjectInputStream(new FileInputStream("class03Object.dat"))) { List objList = new ArrayList<>(); objList.add(input.readObject()); objList.add(input.readObject()); objList.add(input.readDouble()); return objList; } }

/** * Writes the list of DVDs to a random access file */ private static void writeToRAFFile(List dvds) throws IOException { try (RandomAccessFile raf = new RandomAccessFile("class03RAF.dat", "rw")) { for (DVD dvd : dvds) { raf.writeUTF(dvd.getName()); raf.writeUTF(dvd.getLengthOfFilm()); raf.writeUTF(dvd.getRating()); } } }

/** * Reads the list of DVDs from a random access file */ private static List readFromRAFFile() throws IOException { List dvdList = new ArrayList<>(); try (RandomAccessFile raf = new RandomAccessFile("class03RAF.dat", "r")) { while (raf.getFilePointer() < raf.length()) { String name = raf.readUTF(); String lengthOfFilm = raf.readUTF(); String rating = raf.readUTF(); dvdList.add(new DVD(name, lengthOfFilm, rating)); } } return dvdList; }

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Expert Oracle Database Architecture

Authors: Thomas Kyte, Darl Kuhn

3rd Edition

1430262990, 9781430262992

More Books

Students also viewed these Databases questions