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

QUESTION

This part is an extension of Part 2 of Homework 1. The extension includes: Define a class named Painting as a subclass of the Artwork class, which...

This part is an extension of Part 2 of Homework 1. The extension includes:

1.          Define a class named Painting as a subclass of the Artwork class, which you defined in Homework 1.

2.          Store paintings in a singly linked list (instead of in an array). You must use the SinglyLinkedList.java class that is posted on Blackboard along with this assignment (this is slightly different from the SinglyLinkedList.java file that comes with the textbook). You must not modify this file.

3.          Define additional operations.

First, define the Painting class following the requirements given below. This class must be defined in a separate file named Painting.java.

•      Class Painting, which is a subclass of Class Artwork

•      Instance variables

•      paintType: String type // oil, watercolor, etc.

•      material: String type // material it is drawn on, such as paper, canvas, wood, etc.

•      A constructor

•      Painting (String id, String artist, int year, String location, String paintType, String material)

•      Input parameter: id, artist, year, location, paintType, material

•      Precondition:

•      year is a positive integer

•      Postcondition: A new Painting object with given attributes has been created.

•      Methods

•      getPaintType( )

•      Input parameter: None

•      Output: The paintType of the painting

•      getMaterial( )

•      Input parameter: None

•      Output: The material of the painting

•      setPaintType(String paintType)

•      Input parameter: paintType

•      Output: None

•      Postcondition: The paintType of the painting was set to the given paintType.

•      setMaterial(String material)

•      Input parameter: material

•      Output: None

•      Postcondition: The material of the painting was set to the given material.

•    You may define additional methods if you want.

Next, modify the ArtworkManagement.java file to perform the following operations. When your program starts, it must display the following main menu:

Choose an option:

1.    Add a painting

2.    Remove a painting

3.    Update a painting location

4.    Display all paintings

5.    Exit

If the user chooses option 1, your program performs the following:

•      Prompt the user to enter an id and read it.

•      If the artwork with the given id is already in the singly linked list, issue an appropriate error message and redisplay the main menu.

•      If there is no painting with the given id, prompt and read all remaining attributes one at a time.

•      Then, create a painting object and add it to the singly linked list and display an appropriate message indicating a successful operation, and redisplay the main menu.

If the user chooses option 2, your program performs the following:

•      Prompt the user to enter the id of a painting and read it.

•      If there is no painting with the given id, issue an appropriate error message and redisplay the main menu.

•      Otherwise, remove the painting with the given id from the singly linked list and display appropriate message indicating a successful operation, and redisplay the main menu

If the user chooses option 3, your program performs the following:

•      Prompt the user to enter the id of a painting, and read it.

•      If there is no painting with the given id in the singly linked list, issue an error message and redisplay the main menu.

•      If there is a painting with the given id, then prompt the user to enter a new location, and read it.

•      Update the painting's location and issue an appropriate message indicating successful update, and redisplay the main menu.

If the user chooses option 4, display information of all paintings in the singly linked list and redisplay the main menu. For each painting, you must display the following:

id: artist: year: location:

paint type: material:

If the user chooses option 5, terminate the program after displaying an appropriate message, such as "Bye."

//Start SinglyList Java Code

/*

 * Copyright 2014, Michael T. Goodrich, Roberto Tamassia, Michael H. Goldwasser

 *

 * Developed for use with the book:

 *

 *  Data Structures and Algorithms in Java, Sixth Edition

 *  Michael T. Goodrich, Roberto Tamassia, and Michael H. Goldwasser

 *  John Wiley & Sons, 2014

 *

 * This program is free software: you can redistribute it and/or modify

 * it under the terms of the GNU General Public License as published by

 * the Free Software Foundation, either version 3 of the License, or

 * (at your option) any later version.

 *

 * This program is distributed in the hope that it will be useful,

 * but WITHOUT ANY WARRANTY; without even the implied warranty of

 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the

 * GNU General Public License for more details.

 *

 * You should have received a copy of the GNU General Public License

 * along with this program. If not, see <http://www.gnu.org/licenses/>.

 */

package net.datastructures;

/**

 * A basic singly linked list implementation.

 *

 * @author Michael T. Goodrich

 * @author Roberto Tamassia

 * @author Michael H. Goldwasser

 */

public class SinglyLinkedList<E> implements Cloneable {

 //---------------- nested Node class ----------------

 /**

  * Node of a singly linked list, which stores a reference to its

  * element and to the subsequent node in the list (or null if this

  * is the last node).

  */

 public static class Node<E> {

  /** The element stored at this node */

  private E element;      // reference to the element stored at this node

  /** A reference to the subsequent node in the list */

  private Node<E> next;     // reference to the subsequent node in the list

  /**

   * Creates a node with the given element and next node.

   *

   * @param e the element to be stored

   * @param n reference to a node that should follow the new node

   */

  public Node(E e, Node<E> n) {

   element = e;

   next = n;

  }

  // Accessor methods

  /**

   * Returns the element stored at the node.

   * @return the element stored at the node

   */

  public E getElement() { return element; }

  /**

   * Returns the node that follows this one (or null if no such node).

   * @return the following node

   */

  public Node<E> getNext() { return next; }

  // Modifier methods

  /**

   * Sets the node's next reference to point to Node n.

   * @param n  the node that should follow this one

   */

  public void setNext(Node<E> n) { next = n; }

 } //----------- end of nested Node class -----------

 // instance variables of the SinglyLinkedList

 /** The head node of the list */

 public Node<E> head = null;        // head node of the list (or null if empty)

 /** The last node of the list */

 public Node<E> tail = null;        // last node of the list (or null if empty)

 /** Number of nodes in the list */

 private int size = 0;           // number of nodes in the list

 /** Constructs an initially empty list. */

 public SinglyLinkedList() { }       // constructs an initially empty list

 // access methods

 /**

  * Returns the number of elements in the linked list.

  * @return number of elements in the linked list

  */

 public int size() { return size; }

 /**

  * Tests whether the linked list is empty.

  * @return true if the linked list is empty, false otherwise

  */

 public boolean isEmpty() { return size == 0; }

 /**

  * Returns (but does not remove) the first element of the list

  * @return element at the front of the list (or null if empty)

  */

 public E first() {       // returns (but does not remove) the first element

  if (isEmpty()) return null;

  return head.getElement();

 }

 /**

  * Returns (but does not remove) the last element of the list.

  * @return element at the end of the list (or null if empty)

  */

 public E last() {       // returns (but does not remove) the last element

  if (isEmpty()) return null;

  return tail.getElement();

 }

 // update methods

 /**

  * Adds an element to the front of the list.

  * @param e the new element to add

  */

 public void addFirst(E e) {        // adds element e to the front of the list

  head = new Node<>(e, head);       // create and link a new node

  if (size == 0)

   tail = head;              // special case: new node becomes tail also

  size++;

 }

 /**

  * Adds an element to the end of the list.

  * @param e the new element to add

  */

 public void addLast(E e) {         // adds element e to the end of the list

  Node<E> newest = new Node<>(e, null);  // node will eventually be the tail

  if (isEmpty())

   head = newest;             // special case: previously empty list

  else

   tail.setNext(newest);         // new node after existing tail

  tail = newest;              // new node becomes the tail

  size++;

 }

 /**

  * Removes and returns the first element of the list.

  * @return the removed element (or null if empty)

  */

 public E removeFirst() {          // removes and returns the first element

  if (isEmpty()) return null;       // nothing to remove

  E answer = head.getElement();

  head = head.getNext();          // will become null if list had only one node

  size--;

  if (size == 0)

   tail = null;              // special case as list is now empty

  return answer;

 }

 @SuppressWarnings({"unchecked"})

 public boolean equals(Object o) {

  if (o == null) return false;

  if (getClass() != o.getClass()) return false;

  SinglyLinkedList other = (SinglyLinkedList) o;  // use nonparameterized type

  if (size != other.size) return false;

  Node walkA = head;                // traverse the primary list

  Node walkB = other.head;             // traverse the secondary list

  while (walkA != null) {

   if (!walkA.getElement().equals(walkB.getElement())) return false; //mismatch

   walkA = walkA.getNext();

   walkB = walkB.getNext();

  }

  return true;  // if we reach this, everything matched successfully

 }

 @SuppressWarnings({"unchecked"})

 public SinglyLinkedList<E> clone() throws CloneNotSupportedException {

  // always use inherited Object.clone() to create the initial copy

  SinglyLinkedList<E> other = (SinglyLinkedList<E>) super.clone(); // safe cast

  if (size > 0) {          // we need independent chain of nodes

   other.head = new Node<>(head.getElement(), null);

   Node<E> walk = head.getNext();   // walk through remainder of original list

   Node<E> otherTail = other.head;   // remember most recently created node

   while (walk != null) {       // make a new node storing same element

    Node<E> newest = new Node<>(walk.getElement(), null);

    otherTail.setNext(newest);   // link previous node to this one

    otherTail = newest;

    walk = walk.getNext();

   }

  }

  return other;

 }

 public int hashCode() {

  int h = 0;

  for (Node walk=head; walk != null; walk = walk.getNext()) {

   h ^= walk.getElement().hashCode();   // bitwise exclusive-or with element's code

   h = (h << 5) | (h >>> 27);       // 5-bit cyclic shift of composite code

  }

  return h;

 }

 /**

  * Produces a string representation of the contents of the list.

  * This exists for debugging purposes only.

  */

 public String toString() {

  StringBuilder sb = new StringBuilder("(");

  Node<E> walk = head;

  while (walk != null) {

   sb.append(walk.getElement());

   if (walk != tail)

    sb.append(", ");

   walk = walk.getNext();

  }

  sb.append(")");

  return sb.toString();

 }

}

//Start ArtworkManagement Code

/*

 * To change this license header, choose License Headers in Project Properties.

 * To change this template file, choose Tools | Templates

 * and open the template in the editor.

 */

package artworkmanagement;

import java.util.*;//for scanner and stringtokenizer classes

import java.io.*;//for file class and ioexception.

class Artwork{//class named Artwork

String id,artist,location;

int year;

public void details(){//this method prints the art details.

System.out.println(id+" "+artist+" "+year+" "+location);

}

}

//main class named ArtworkManagement

class ArtworkManagement{

//findArtwork method  

static int findArtwork(Artwork art[],int n,String artID){

for(int i=0;i<n;i++){//searches the array for the artID and

if((art[i].id).equals(artID))//if it is found then

return i;//returns the array for the artID

}

return -1;//if not found -1 is returned.

}

//main method throws IOException as we are dealing with file.

public static void main(String args[])throws IOException{

File file = new File("artwork_info.txt");//opens file named artwork_info.txt

Scanner read = new Scanner(file);//read object of Scanner class for reading from file.

Scanner input = new Scanner(System.in);//input object of Scanner class for taking input from user.

int n = read.nextInt();//reads the integer in first line.

read.nextLine();//reads the extra spaces after the integer in first line.

Artwork art[] = new Artwork[n];//created an array of objects of class Artwork.

for(int i=0;i<n;i++){//loops from 0 to n-1

art[i] = new Artwork();

String data = read.nextLine();//reads line from the file.

//tokenizes the data string by delimiter ,

StringTokenizer st = new StringTokenizer(data,",");

while(st.hasMoreTokens()){//loop continues till st has more tokens available.

art[i].id = st.nextToken();

art[i].artist = st.nextToken();

//the year in the file has space infront of it to remove it another stringtokenizer is used.

StringTokenizer space = new StringTokenizer(st.nextToken()," ");

art[i].year = Integer.parseInt(space.nextToken());

art[i].location = st.nextToken();

}

}

//from here the menu will work as stated in problem.

int choice;

do{

System.out.print("n1. Artwork informationn2. Update locationn3. Exit: ");

choice = input.nextInt();

if(choice==1){

System.out.print("Enter id of an art work: ");

String artID = input.next();

int result = findArtwork(art, n, artID);

if(result == -1)

System.out.print("error: There is no such Artwork with the entered IDn");

else{

art[result].details();

}

}

else if(choice==2){

System.out.print("Enter id of an art work: ");

String artID = input.next();

int result = findArtwork(art, n, artID);

if(result == -1)

System.out.print("error: There is no such Artwork with the entered IDn");

else{

System.out.print("nEnter new location: ");

String loc = input.next();

art[result].location = loc;

art[result].details();

}

}

}while(choice==1||choice==2);//this do while loop continues iteration till user enters 1 or 2.

System.out.println("Bye");//prints Bye at the end.

}

}

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