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

Junit test inclueded. ----------------------------------------------------------

ID: 3887401 • Letter: J

Question

Junit test inclueded.

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

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.

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

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

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;
}
}

Explanation / Answer

Java code with methods required (Rate if satisfied else comment for queries)

package com.chegg;

public class JunitMethods {

public int number;

public String name;

public static TitleInfo bookTitle;

/**

* getNumber() is instance-based, does not have any parameters, and returns

* an int. It should return the value of the number field.

*

* @return

*/

public int getNumber() {

return number;

}

/**

* getName() is instance-based, does not have any parameters, and returns a

* String. It should return the value of the name field.

*

* @return

*/

public String getName() {

return name;

}

/**

* 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.

*

* @param inputName

*/

public void setName(String inputName) {

name = inputName;

}

/**

* 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.

*

* @return

*/

public static String getBookTitle() {

return bookTitle.getBookTitle() + ", " + bookTitle.getEdition() + " 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.

*

*

* @param title

* @param edition

*/

public static void setBookInfo(String title, int edition) {

bookTitle.setBookTitle(title);

bookTitle.setEdition(edition);

}

}

TitleInfo class:

package com.chegg;

public class TitleInfo {

public String title;

public int edition;

public TitleInfo(String bookTitle, int edition) {

this.title = bookTitle;

this.edition = edition;

}

public String getBookTitle() {

return title;

}

public void setBookTitle(String bookTitle) {

this.title = bookTitle;

}

public int getEdition() {

return edition;

}

public void setEdition(int edition) {

this.edition = edition;

}

}