1) a) create a method called sum in class Node ( Node classe given below), that
ID: 3560085 • Letter: 1
Question
1)
a) create a method called sum in class Node ( Node classe given below), that will recursively calculate the sum of all of the int data values from that node to the last node in the list. (HINT: using the next pointer) This method should return (NOT PRINT OUT) the sum that was calculated.
b) IN CLASS LINKED LIST, create a method called sum that will take an int, n, as a parameter. It will call method sum (of class Node) on the nth node in the list. (the head is node 1, the node after head is 2, etc.) And then print out to a file the sum that was calculated. If the parameter passed is negative or zero, then the sum should be zero. If n is greater than the number of nodes in the list, then throw an appropriate exception.
2)
a) create a method called printBackwards in the same Node class. The method should take as a parameter an object of type PrintWriter. It should be a recursive method. The purpose of the method is to print all of the values from all of the nodes from the current node (as in the this keyword) until the end of the list IN BACKWARDS ORDER. In other words, the last node in the list will appear at the beginning of the output text file, and the current node will appear later in the text file.
b) create a method printB in the same LinkedList class. The method should create an instance of PrintWriter and call the method printBackwards from class Node.
MAKE SURE that you use different output files for part 1 and part 2.
B) Using class Shape (Given below) that you created from assignment 5. Change class Shape to implent the interface Comparable. The Shape objects should be compared based on their areas. Make sure to use the Generic Comparable.
C) Create a main class called Assignment 7. The main method should read from the file that is attached to this assignment. The main method should create a new LinkedList Object, using the number 6 as the value passed to the constructor. Then the main method should read from the file and for each numeric value read it should add that number to the list. Then the method should call methods sum and printB of class LinkedList.
Node Class:
public class Node {
private int data;
private Node next;
public Node (int d){
data = d;
}
public Node (int d, Node n) {
data = d;
next =n;
}
public int getData() {
return data;
}
public Node getNext () {
return next;
}
public void setNext(Node n) {
next = n;
}
}
LinkedList Class:
public class LinkedList {
private Node head;
public LinkedList(Node n){
head =n;
}
public LinkedList(int x){
head = new Node(x);
}
public void append(int x) {
Node current = head;
while (current.getNext()!=null){
current = current.getNext();
}
current.setNext(new Node(x));
}
public void insert(int x) {
if(head.getData()>x){ // case 1: insert before head
head = new Node(x, head);
} else {
Node current = head;
Node previous = head;
while (current!=null){
if (current.getData()>x){ // case 2: insert into the middle of the list
previous.setNext(new Node(x, current));
// Node temp = new Node(x, current);
// previous.setNext(temp);
break;
} else {
previous = current;
current = current.getNext();
} // else
} // end of while
if(current == null){ // case 3: insert after list
previous.setNext(new Node(x));
} // if
} // else
} // insert method
public boolean delete(int x){
if(x==head.getData()){
head = head.getNext();
return true;
}
Node current = head;
Node previous = head;
while (current!=null){
if (current.getData()==x){ // case 2: delete from the middle or from the end
previous.setNext(current.getNext());
return true;
} else if(current.getData()>x) {
return false;
} else {
previous = current;
current = current.getNext();
}
}// while
return false;
} // delete method
Shape class:
public abstract class Shape {
protected double area;
protected double perimeter;
public double getArea() {
return area;
}
public double getPerimeter() {
return perimeter;
}
public abstract double calculateArea();
public abstract double calculatePerimeter();
public abstract void resetDimensions();
}
Explanation / Answer
Node Class:
public class Node {
private int data;
private Node next;
public Node (int d){
data = d;
}
public Node (int d, Node n) {
data = d;
next =n;
}
public int getData() {
return data;
}
public Node getNext () {
return next;
}
public void setNext(Node n) {
next = n;
}
}
LinkedList Class:
public class LinkedList {
private Node head;
public LinkedList(Node n){
head =n;
}
public LinkedList(int x){
head = new Node(x);
}
public void append(int x) {
Node current = head;
while (current.getNext()!=null){
current = current.getNext();
}
current.setNext(new Node(x));
}
public void insert(int x) {
if(head.getData()>x){ // case 1: insert before head
head = new Node(x, head);
} else {
Node current = head;
Node previous = head;
while (current!=null){
if (current.getData()>x){ // case 2: insert into the middle of the list
previous.setNext(new Node(x, current));
// Node temp = new Node(x, current);
// previous.setNext(temp);
break;
} else {
previous = current;
current = current.getNext();
} // else
} // end of while
if(current == null){ // case 3: insert after list
previous.setNext(new Node(x));
} // if
} // else
} // insert method
public boolean delete(int x){
if(x==head.getData()){
head = head.getNext();
return true;
}
Node current = head;
Node previous = head;
while (current!=null){
if (current.getData()==x){ // case 2: delete from the middle or from the end
previous.setNext(current.getNext());
return true;
} else if(current.getData()>x) {
return false;
} else {
previous = current;
current = current.getNext();
}
}// while
return false;
} // delete method
Shape class:
public abstract class Shape {
protected double area;
protected double perimeter;
public double getArea() {
return area;
}
public double getPerimeter() {
return perimeter;
}
public abstract double calculateArea();
public abstract double calculatePerimeter();
public abstract void resetDimensions();
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.