Static methods can be called directly from the name of the class
that contains the method. Static methods are usually created to do
utility operations. For example:
public class Test{
public static int timesTwo(int value){
return value * value;
}
}
public class TestDriver{
public static void main(String[] args){
int var = Test.timesTwo(5);
System.out.println(var);
}
}
For your final exercise, create a class called ManyLinkedLists. It
will contain a static method called createLinkedList(). That method
takes an argument that is a constant defined in the ManyLinkedLists
class. The identifier of the constants should describe a type of
linked list. When the createLinkedList method is called, it should
return a linked list object of the type identified by the
constant.
For example:
DoubleEndedList del =
ManyLinkedLists.createLinkedList(ManyLinkedLists.DOUBLEENDEDLIST);
Give the createLinkedList method the ability to return the linked
lists described below:
Use a long instance variable as the data within your link class or
classes.
Your project should contain any other classes you need. Include a
ManyLinkedListsDriver class that tests the ManyLinkedLists class,
its method, and the data structures that the method can return. The
peculiar features of the two forms of linked list should be clearly
demonstrated.
Include a document in your submitted project that clearly explains the difference between a double-ended linked list and a doubly linked list.
The Linked List that is mentioned in the question must possess two features.
1. Doubly Linked list: The Doubly Linked list the one in which the node has pointers to both next pointer and the previous pointer. Whereas in case of a singly linked list, the node has only the next pointer.
2. Double Ended Linked List: The double ended linked list is similar to that of a deque. The elements can be inserted from either side of a list.
Implementation: The node is has a data associated with the pointers. Hence we have to make a class called Node. We must also specify the methods that are required for inserting or deleting the elements to the list.
class Node{
public: int data;
Node next;
Node prev;
public:
Node(int data){
this.data = data;
this.next = null;
this.prev = null;
}
}
class Deque{
public: Node head, tail;
public void addFirst(int data){
Node node = new Node(data);
if(head == null){
head = node;
tail = node;
}else{
node.next = head;
head.prev = node;
head = node;
}
}
public void addLast(int data){
Node node = new Node(data);
if(head == null){
head = node;
tail = node;
}else{
tail.next = node;
node.prev = tail;
tail = node;
}
}
public void pollFirst(){
if(head == null) System.out.println("List is empty");
if(head == tail){
head = tail = null;
}
else{
Node temp = head;
head = head.next;
temp.next = null;
head.prev = null;
}
}
public void pollLast(){
if(head == null) System.out.println("List is empty");
if(head == tail){
head = tail = null;
}
else{
Node temp = tail;
tail = tail.prev;
temp.prev = null;
tail.next = null;
}
}
}
class ManyLinkedLists{
Node createLinkedList(){
Deque d = new Deque();
return d.head; // returning the head of the list
}
}
Static methods can be called directly from the name of the class that contains the method....
Static methods can be called directly from the name of the class that contains the method. Static methods are usually created to do utility operations. For example: public class Test{ public static int timesTwo(int value){ return value * value; } } public class TestDriver{ public static void main(String[] args){ int var = Test.timesTwo(5); System.out.println(var); } } For your final exercise, create a class called ManyLinkedLists. It will contain a static method called createLinkedList(). That method takes an argument that is...
Java:
Create the skeleton.
Create a new file called ‘BasicJava4.java’
Create a class in the file with the appropriate name.
public class BasicJava4 {
Add four methods to the class that return a default value.
public static boolean isAlphabetic(char aChar)
public static int round(double num)
public static boolean useSameChars(String str1, String
str2)
public static int reverse(int num)
Implement the methods.
public static boolean isAlphabetic(char aChar):
Returns true if the argument is an alphabetic character, return
false otherwise. Do NOT use...
Implement the following method as a new static method for the IntNode class. (Use the usual IntNode class with instance variables called data and link.) public static int count42s(IntNode head) // Precondition: head is the head reference of a linked list. // The list might be empty or it might be non-empty. // Postcondition: The return value is the number of occurrences // of 42 in the data field of a node on the linked list. // The list itself...
C++ This exercise will introduce static member variables and static methods in class to you. Class Department contain information about universities departments: name students amount in addition it also stores information about overall amount of departments at the university: departments amount class Department { public: Department(string i_name, int i_num_students); ~Department(); int get_students(); string get_name(); static int get_total(); private: string name; int num_students; static int total_departments; }; Carefully read and modify the template. You have to implement private static variable "total...
This class implements a doubly linked list in which the nodes are of the class DLNode. This class must implement the interface DLListADT.java that specifies the public methods in this class. The header for this class will then be public class DLList implements DLListADT This class will have three private instance variables: • private DLNode front. This is a reference to the first node of the doubly linked list. • private DLNode rear. This is a reference to the last...
List of Candles Create a class called CandleNode which has fields for the data (a Candle) and next (CandleNode) instance variables. Include a one-argument constructor which takes a Candle as a parameter. (For hints, see the PowerPoint on "Static vs. Dynamic Structures”.) public CandleNode (Candle c) { . . } The instance variables should have protected access. There will not be any get and set methods for the two instance variables. Create an abstract linked list class called CandleList. This...
Develop a set of static methods in a class called Array Tools that perform the functions below, and overload the methods for the specified types: Description Returns the minimum value stored in an array. Array Tools Method char minimum char array[]) int minimum( int array[] ) double minimum double arrayll ) char maximum char array() ) int maximum( int array[] ) double maximum( double array[] ) Returns the maximum value stored in an array. int minimumAt( char array()) int minimumAt(...
Create a class called CarNode which has fields for the data (a Car) and next (CarNode) instance variables. Include a one-argument constructor which takes a Car as a parameter. (For hints, see the PowerPoint on "Static vs. Dynamic Structures”.) public CarNode (Car c) { . . } The instance variables should have protected access. There will not be any get and set methods for the two instance variables. Create an abstract linked list class called CarList. This should be a...
9) Write out the following method as a static method for the IntNode class (similar to the book: use data & link) public static int count10s (IntNode head) I Precondition: Head is a reference to a linked list of integers, Il condition of the list is unknown (empty or not) // Postcondition: Method returns number of 10s found in the linked list. / list is unchanged. 10) Write out the following method as a static method for the IntNode class...
Create a class called CompareArrays that determines if two specified integer arrays are equal. The class should have one static methods: public static boolean compare(int[] arrayOne, int[] arrayTwo) – Which compares the two arrays for equality. The two arrays are considered equal if they are the same size, and contain the same elements in the same order. If they are equal, the method should return true. Otherwise, the method should return false. Create a second class called CompareArraysTest that contains...