Answered You can hire a professional tutor to get the answer.

QUESTION

This is my goal: Provide Java code for a simple class of your choice. Be sure to include at least one constructor, two methods and two fields. The...

This is my goal: Provide Java code for a simple class of your choice. Be sure to include at least one constructor, two methods and two fields. The fields should be private.

Create a test class to constuct and call the methods of your class.

Describe your class and demonstrate your code functions properly.

I created this code and attached the files. However my output is not the way i am trying to get it. Its also pretty long and i dont know how to make it more better because this is not my strongest subject

i would like it to be as follows:

Your book is a: mystery/fiction/nonfiction

your was published: before 2008

you book is: available in hardcover

Your book is available: TRUE AND/OR FALSE

Your book is: Available in less than five copies (if random generator is below 5)

Your book is: Available in less than six copies (if random generator is below 6)

your was published: before/after 2008

Your book is a: mystery/fiction/nonfiction

Here are the codes:

/* Author: 

*File: Books.java

*Author:  

* Date: 

* Purpose: This program states the type(genre), whether it's a hardcover, availability and how long ago the book was published. 

 */

import java.util.Random;

public class Books {

  //declare fields

  private String bookType;

  private int bookAge;

  private boolean bookHardcover;

  private String currentActivity;

  private int nextActivity;

  private Random bookAvail;

  //constructor, initializes fields and holds logic for what kind of book it is  

  public Books(int bookNum) {

    bookAvail = new Random();

    bookAge = 0;

    bookHardcover= true;

    currentActivity = "available in a hardcover";

    if (bookNum < 3) {

      bookType = "Mystery";

    } else if (bookNum < 7) {

      bookType = "Fiction";

    } else {

      bookType = "NonFiction";

    }

  }

  //default constructor, generates random "bookNum" between 0 and 10

  public Books () {

    this((int)(Math.random() * 10));

  }

  //methods to see class field states

  public String getType() {

    return bookType;

  }

  public int getAge() {

    return bookAge;

  }

  public boolean isHardcover() {

    return bookHardcover;

  }

  //has condition to check and see if book has been checked out

  public String watchBook() {

    if (currentActivity == null) {

      return "Your book is checked out";

    } else {

      return currentActivity;

    }

  }

  //methods to interact with "books"

  //sets hardcover to false and generates new activity

  //if condition checks to see if book is available in paperback

  public void checkBook() {

    if (currentActivity != null) {

      bookHardcover = false;

      nextActivity = bookAvail.nextInt(10); //picks new random 0 - 10 for following logic

      if (nextActivity < 5) {

        currentActivity = "available in less than 5 paperback copies!";

      } else if (nextActivity < 6) {

        currentActivity = "available in less than 6 hardcover copies!";

      } else {

        currentActivity = "currently checked out";

      }

    }

  }

  //increases copies or checks availability of the book by setting activity field to null

  //will only increase copies if book is available or not, otherwise exits the method early.

  public void checkkBookk() {

    if (bookHardcover == true) {

      System.out.println("available in a hardcover!");

      return;

    }

    if (currentActivity == null) {

      System.out.println("Your book is not available at this library!");

      return;

    }

    if (bookAge> 9) {

      System.out.println("Your book was published before 2008.");

      currentActivity = null;

    } else {

      bookAge++;

      bookHardcover = true;

    }

  }

}

/* Author: 

*File: TestBooks.java

*Author: Roxanna Barahona 

* Date: March 16, 2018

* Purpose: FILL IN LATER 

 */

public class TestBooks {

  public static void main (String args[]){

    Books book1 = new Books(); //generate new book with no parameters

    //lists initial fields

    System.out.println("Your book is a: " + book1.getType());

    System.out.println("Your book is availabe in this many copies:" + book1.getAge());

    System.out.println("Your book is: " + book1.watchBook());

    System.out.println("Your book is: " + book1.isHardcover());

    //advances the book 1 year and lists new fields

    book1.checkBook();

    book1.watchBook();

    System.out.println("Your book is availabe in this many copies: " + book1.getAge());

    System.out.println("Your book is: " + book1.watchBook());

    //lists new field again after book gets checked

    book1.checkBook();

    System.out.println("Your book is: " + book1.watchBook());

    book1.checkkBookk();

    //loops to max book age, 

    for (int i = 0; i < 9; i++) {

      book1.checkBook();

      book1.checkkBookk();

    }

    //lists final book stats

    System.out.println("Your book is: " + book1.watchBook());

    System.out.println("Your book is availabe in this many copies: " + book1.getAge());

    //second constructor usage and demonstration

    Books book2 = new Books(2);

    System.out.println("Your book is a: " + book2.getType());

  }

}

Show more
LEARN MORE EFFECTIVELY AND GET BETTER GRADES!
Ask a Question