Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Junit test inclueded, and also Be sure to finish chapter method. ---------------------------------------------------------------------------------------------- It is critical you match my field names, types, and whether or not

Junit test inclueded, and also Be sure to finish chapter method.

----------------------------------------------------------------------------------------------

It is critical you match my field names, types, and whether or not each is static.

name should be of type String and be instance-based;

number should be of type int and be instance-based;

bookTitle should be of type TitleInfo and NOT be instance-based; and

totalChapters should be of type int and NOT be instance-based.

You will also need to define 5 (very simple) methods. As with the fields, your methods MUST have the same method names, parameter and return types, and be static/instance-based as specified here.

getNumber() is instance-based, does not have any parameters, and returns an int. It should return the value of the number field.

getName() is instance-based, does not have any parameters, and returns a String. It should return the value of the name field.

setName() is instance-based, has 1 parameter of type String, and does NOT return anything. It should reassign name so that it is equal to the parameter's value.

getBookTitle() is NOT instance-based, does not have any parameters, and returns a String. It should return the String containing the book's title, a comma, a space, the book's edition number, another space, and then the word "edition". So examples would include: Hertz is the greatest, 3 edition or The Book of Hertz, 17 edition.

setBookInfo() is NOT instance-based, does not return anything, and has 2 parameters. The first parameter is a String and the second parameter is an int. The method should reassign bookTitle so that aliases a new TitleInfo instance with a title of the String parameter and edition number of the int parameter.

------------------------------------------------------------------------------------------------------------

package edu.buffalo.cse116; /** * Instances of this class represents a chapter in a single book. Since the book's properties are defined as static * fields of this class, it is usable by a program which only defines a single book. * * @author Matthew Hertz */ public class Chapter { /** * Add a new chapter to the end of this book. The Chapter instance should have the name stored in {@code chptName} and * set its number to appear so that it is at the end of the book. * * @param chptName String storing the name to be used by this Chapter. */ public Chapter(String chptName) { } } 

------------------------------------------------------------------

Junit Test(1/2)

package edu.buffalo.cse116;

import static org.junit.Assert.*;

import java.lang.reflect.Field; import java.lang.reflect.Method; import java.lang.reflect.Modifier;

import org.junit.Before; import org.junit.Test;

public class ChapterTest {

@Test public void fieldsAndMethodsCorrect() { // This is done in the @Before method, but this gives students credit for doing this. }

@Test public void testChapterConstructor() throws Exception { setTotalChapters(14); Chapter seventeenthChapter = new Chapter("Graphs, Tree, and Networks"); String chapterName = getName(seventeenthChapter); int chapterNum = getNumber(seventeenthChapter); int totalChapters = getTotalChapters(); assertEquals("Should set the Chapter instance's name field equal to the String in the constructor's parameter", "Graphs, Tree, and Networks", chapterName); assertEquals("Should add one to the value of Chapter's totalChapters field in the constructor", 15, totalChapters); assertEquals("Should set the Chapter instance's number field equal to the value of totalChapters AFTER it has been increased by 1", 15, chapterNum); TitleInfo ti = getBookTitle(); assertNull("Should not update the bookTitle field in Chapter's constructor", ti); setTotalChapters(4); Chapter fifthChapter = new Chapter("Recursion"); chapterName = getName(fifthChapter); chapterNum = getNumber(fifthChapter); totalChapters = getTotalChapters(); assertEquals("Should set the Chapter instance's name field equal to the String in the constructor's parameter", "Recursion", chapterName); assertEquals("Should add one to the value of Chapter's totalChapters field in the constructor", 5, totalChapters); assertEquals("Should set the Chapter instance's number field equal to the value of totalChapters AFTER it has been increased by 1", 5, chapterNum); ti = getBookTitle(); assertNull("Should not update the bookTitle field in Chapter's constructor", ti); }

@Test public void testGetNumber() throws Exception { Chapter firstChapter = new Chapter("OO Concepts"); setNumber(firstChapter, 1); // Who says the title should be meaningful? Chapter seventhChapter = new Chapter("OO Concepts"); setNumber(seventhChapter, 7);

int num = firstChapter.getNumber(); assertEquals("Should return the value of the Chapter instance's name field in getNumber()", 1, num);

num = seventhChapter.getNumber(); assertEquals("Should return the value of the Chapter instance's name field in getNumber()", 7, num); }

@Test public void testGetName() throws Exception { Chapter two = new Chapter("OO Concepts"); Chapter five = new Chapter("Recursion"); setName(two, "Introduction"); setName(five, "OO Concepts"); String name = two.getName(); assertEquals("getName() should return the value stored in name", "Introduction", name); name = five.getName(); assertEquals("getName() should return the value stored in name", "OO Concepts", name); }

@Test public void testSetName() throws Exception { Chapter two = new Chapter("OO Concepts"); Chapter five = new Chapter("Recursion"); two.setName("Introduction"); five.setName("OO Concepts"); String name = getName(two); assertEquals("getName() should return the value stored in name", "Introduction", name); name = getName(five); assertEquals("getName() should return the value stored in name", "OO Concepts", name); }

@Test public void testGetBookTitle() throws Exception { setBookTitle(new TitleInfo("Book Titles are Hard", 7)); try { String formattedTitle = Chapter.getBookTitle(); assertEquals("getBookTitle() should return the String as specified in the assignment", "Book Titles are Hard, 7 edition", formattedTitle); setBookTitle(new TitleInfo("Test Examples are Harder", 13)); formattedTitle = Chapter.getBookTitle(); assertEquals("getBookTitle() should return the String as specified in the assignment", "Test Examples are Harder, 13 edition", formattedTitle); } catch (Exception e) { fail("Should not crash due to calling getBookTitle(), but instead received: " + e.toString()); e.printStackTrace(); } }

@Test public void testSetBookInfo() throws Exception { Chapter.setBookInfo("Hertz is the man", 3); TitleInfo ti = getBookTitle(); String title = ti.getTitle(); assertEquals("Should create a new TitleInfo with a title equal to the first parameter in setBookInfo()", "Hertz is the man", title); int ed = ti.getEdition(); assertEquals("Should create a new TitleInfo whose edition number is equal to the second parameter in setBookInfo()", 3, ed); Chapter.setBookInfo("CSE116 is fun!", -1); TitleInfo ti3 = getBookTitle(); assertNotSame("Should create a new instance for bookTitle in setBookInfo()", ti, ti3); title = ti3.getTitle(); assertEquals("Should create a new TitleInfo with a title equal to the first parameter in setBookInfo()", "CSE116 is fun!", title); ed = ti3.getEdition(); assertEquals("Should create a new TitleInfo whose edition number is equal to the second parameter in setBookInfo()", -1, ed); } private Field numberField; private Field nameField; private Field bookTitleField; private Field totalChaptersField;

@Before public final void checkFieldsAndMethodsDefined() { Class chapterKlass = Chapter.class; Field[] fields = chapterKlass.getDeclaredFields(); assertEquals("You should only declare the 4 required fields in the Chapter class. Your class's field count: ", 4, fields.length); try { numberField = chapterKlass.getDeclaredField("number"); numberField.setAccessible(true); assertTrue("Chapter's number field should have been declared using a type of int", Integer.TYPE == numberField.getType()); assertFalse("Each instance of Chapter should have its own copy of the number field.", Modifier.isStatic(numberField.getModifiers())); } catch (Exception e) { fail("Your Chapter class should define a field named \"number\"");

} try { nameField = chapterKlass.getDeclaredField("name"); nameField.setAccessible(true); assertTrue("Chapter's number field should have been declared using a type of String", String.class == nameField.getType()); assertFalse("Each instance of Chapter should have its own copy of the name field.", Modifier.isStatic(nameField.getModifiers())); } catch (Exception e) { fail("Your Chapter class should define a field named \"name\"");

} try { bookTitleField = chapterKlass.getDeclaredField("bookTitle"); bookTitleField.setAccessible(true); assertTrue("Chapter's bookTitle field should have been declared using a type of TitleInfo", TitleInfo.class == bookTitleField.getType()); assertTrue("Instances of Chapter should share a single copy of the bookTitle field.", Modifier.isStatic(bookTitleField.getModifiers())); // Reset the book title to its initial state setBookTitle(null); } catch (Exception e) { fail("Your Chapter class should define a field named \"bookTitle\""); } try { totalChaptersField = chapterKlass.getDeclaredField("totalChapters"); totalChaptersField.setAccessible(true); assertTrue("Chapter's totalChapters field should have been declared using a type of int", Integer.TYPE == totalChaptersField.getType()); assertTrue("Instances of Chapter should share a single copy of the totalChapters field.", Modifier.isStatic(totalChaptersField.getModifiers())); } catch (Exception e) { fail("Your Chapter class should define a field named \"totalChapters\""); }

try { Class[] empty = {}; Method numberMethod = chapterKlass.getDeclaredMethod("getNumber", empty); assertFalse("Chapter's getNumber() method should be defined so that it includes an implicit \"this\" parameter.", Modifier.isStatic(numberMethod.getModifiers())); } catch (Exception e) { fail("Your Chapter class should define a method named \"getNumber()\""); }

try { Class[] empty = {}; Method nameMethod = chapterKlass.getDeclaredMethod("getName", empty); assertFalse("Chapter's getName() method should be defined so that it includes an implicit \"this\" parameter.", Modifier.isStatic(nameMethod.getModifiers())); } catch (Exception e) { fail("Your Chapter class should define a method named \"getName()\""); } try { Class[] notEmpty = { String.class }; Method nameMethod = chapterKlass.getDeclaredMethod("setName", notEmpty); assertFalse("Chapter's setName(String) method should be defined so that it includes an implicit \"this\" parameter.", Modifier.isStatic(nameMethod.getModifiers())); } catch (Exception e) { fail("Your Chapter class should define a method named \"setName(String)\""); }

try { Class[] empty = {}; Method bookTitleMethod = chapterKlass.getDeclaredMethod("getBookTitle", empty); assertTrue("Chapter's getBookTitle() method should be defined so that it can be called without an instance.", Modifier.isStatic(bookTitleMethod.getModifiers())); } catch (Exception e) { fail("Your Chapter class should define a method named \"getBookTitle()\""); } try { Class[] notEmpty = { String.class, Integer.TYPE }; Method bookInfoMethod = chapterKlass.getDeclaredMethod("setBookInfo", notEmpty); assertTrue("Chapter's setBookInfo(String, int) method should be defined so that it can be called without an instance.", Modifier.isStatic(bookInfoMethod.getModifiers())); } catch (Exception e) { fail("Your Chapter class should define a method named \"setBookInfo(String, int)\""); } }

private void setTotalChapters(int newTotal) throws Exception { totalChaptersField.setInt(null, newTotal); }

private int getTotalChapters() throws Exception { return totalChaptersField.getInt(null); }

private void setBookTitle(TitleInfo newInfo) throws Exception { bookTitleField.set(null, newInfo); }

private TitleInfo getBookTitle() throws Exception { return (TitleInfo) bookTitleField.get(null); }

private void setName(Chapter testee, String newName) throws Exception { nameField.set(testee, newName); }

private String getName(Chapter testee) throws Exception { return (String) nameField.get(testee); }

private int getNumber(Chapter testee) throws Exception { return numberField.getInt(testee); }

private void setNumber(Chapter testee, int newNumber) throws Exception { numberField.setInt(testee, newNumber); } }

---------------------------------------------------------------------------

Junit Test(2/2)

package edu.buffalo.cse116;

/** * Instances of this class define the information used in a book's title. Since a book's title is not expected to * change, this class creates "immutable" (non-changeable) instances. * * @author Matthew Hertz * */ public class TitleInfo { /** Title of the book. */ private String title;

/** * The version of the book. This is especially helpful for textbooks which seem to create a new edition each year (& * so limit used textbook purchases) despite making little or no changes. */ private int edition;

/** * Create a new book title instance. The instance uses the values of the parameters to set its title and edition * number. * * @param bookTitle * String storing the title of the book whose info is being stored. * @param editionNum * Integer storing the book's edition. */ public TitleInfo(String bookTitle, int editionNum) { edition = editionNum; title = bookTitle; }

/** * Get the edition of this book. * * @return Rank in the series of releases of books with an otherwise identical name. */ public int getEdition() { return edition; }

/** * Get the title of this book. * * @return String with the text of the title of this book; this is independent of the book's edition number. */ public String getTitle() { return title; } }

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

Modern Datalog Engines In Databases

Authors: Bas Ketsman ,Paraschos Koutris

1st Edition

1638280428, 978-1638280422

Students also viewed these Databases questions