Java programming language
I have to create a remove method that removes the element at an index (ind) or space in an array and returns it. Thanks!
I have to write the remove method in the code below. i attached the part where i need to write it.
public class ourArrayList<T>{
private Node<T> Head = null;
private Node<T> Tail = null;
private int size = 0;
//default constructor
public ourArrayList() {
Head = Tail = null;
size = 0;
}
public int size() {
return size;
}
public boolean isEmpty() {
return (size == 0);
}
//implement the method add, that adds to the back of the list
public void add(T e) {
//HW TODO and TEST
//create a node and assign e to the data of that node.
Node<T> N = new Node<T>();//N.mData is null and N.next is null as well
N.setsData(e);
//chain the new node to the list
//check if the list is empty, then deal with the special case
if(this.isEmpty()) {
//head and tail refer to N
this.Head = this.Tail = N;
size++;
//return we are done.
return;
}
//The list is not empty
//if the list is not empty. Then update the link of the last element to refer to the
//new node then update Tail to refer to the new node.
Tail.setNext(N);
Tail = N;
//increase size.
size++;
}
//remove removes the first/head and returns it.
public T remove() {
//check if empty then return null
if(this.isEmpty()) {
return null;
}
//it is not empty
//save the returned item
T save = this.Head.getData();
//Head should refer to the next node
this.Head = this.Head.getNext();
if(this.Head == null) {
this.Tail = null;
}
size--;
return save;
}
//T get(intind) returns the element at index ind without removing it.
public T get(int ind) {
//if list is empty return null
////if index is smaller than 0 or larger than size-1 return null
if(this.isEmpty() || ind < 0 || ind > size - 1) {
return null;
}
//create a temp reference, starting at Head.
Node<T> temp = this.Head;
//loop through the list ind times and return the data at the node.
for(int i = 0; i < ind; i++) {
//move to the next
temp = temp.getNext();
}
//temp should refer to the node you are looking for. return the data in that node
return temp.getData();
}
public void add(T e, int ind) {
//if ind is not valid return
if(ind < 0 || ind > size) {
return;
}
//if ind == size just call add(e), add it to the back
if(ind == size) {
//insert at the end just call add(e)
this.add(e);
return;
}
//create a new node with e as data
Node<T> N = new Node<T>(e);
//if ind == 0; //add it ot the head as a special case and return
if(ind == 0) {
N.setNext(this.Head);
Head = N;
size++;
return;
}
//The index is somewhere in between.
//Go to node ind - 1 and insert your Node.
Node<T> temp = this.Head;
for(int i =0; i<ind-1; i++) {
//go to next node
temp = temp.getNext();
}
//Set the next opf new node to next of temp
N.setNext(temp.getNext());
//set next of temp to new node
temp.setNext(N);
size++;
}
//TODO HW: remove the element at index ind and returns it. Test it.
public T remove (int ind) {
return null;
}
public String toString() {
if(this.isEmpty())
return "[]";
String st = "[";
//create a temp reference to the first node.
Node<T> temp = this.Head;
while(temp.getNext() != null) {
//go through all nodes until the last and add the content to the string.
st += temp.toString() + ", ";
//go to the next node
temp = temp.getNext();
}
//temp should poinrt to the last element at this point
st += temp.toString() + "]";
return st;
}
}
Trending nowThis is a popular solution!
Step by stepSolved in 3 steps with 3 images
- Main Class Implement a main class that implements our ParkingOffice.java class. Parkingoffice.java public class ParkingOffice {String name;String address;String phone;String parkingOfficeName;List<Customer> customers;List<Car> cars;List<ParkingLot> lots;List<ParkingCharge> charges; public ParkingOffice(){customers = new ArrayList<>();cars = new ArrayList<>();lots = new ArrayList<>();charges = new ArrayList<>();} public String getParkingOfficeName(){return this.parkingOfficeName;} /* to implement the desired park function */ public String park(Date date,String ParkingPermit,ParkingLot pl){ /* to have the amount paid during the transaction forthe specified parking lot */double amount=pl.amount;/* to have the date on which the parking lot is booked */String onDate=date.toString();/* to know the permit id */String permitId=ParkingPermit;/* to print the transaction receipt */String s="Amount paid on date: ";s=s+onDate;s=s+" is…arrow_forward1 import java.util.Random; 2 3 4 5 6- 7 8 9 10 11 12 ▼ import java.util.StringJoiner; public class preLabC { public static MathVector myMethod(int[] testValues) { // Create an object of type "MathVector". // return the Math Vector Object back to the testing script. return VectorObject; 13 14 15 } } ▸ Comments Description The MathVector.java file contains a class (Math Vector). This class allows you to create objects in your program that can contain vectors (a 1D array) and perform actions on those vectors. This is useful for doing math operations like you would in your linear algebra class. Your method should evaluate against two tests. What do you need to do in this exercise? You need to create an "instance" of the MathVector object, VectorObject, in your method. If you do this correctly, your method will accept a 1D array of integers, testValues, feed it into the VectorObject and then return the VectorObject. If you look in the file MathVector.java you'll see that this is one way in…arrow_forwardAdd the method below to the parking office.java class. Method getParkingCharges(ParkingPermit) : Money Parkingoffice.java public class ParkingOffice {String name;String address;String phone; List<Customer> customers;List<Car> cars;List<ParkingLot> lots;List<ParkingCharge> charges; public ParkingOffice(){customers = new ArrayList<>();cars = new ArrayList<>();lots = new ArrayList<>();charges = new ArrayList<>();}public Customer register() {Customer cust = new Customer(name,address,phone);customers.add(cust);return cust;}public Car register(Customer c,String licence, CarType t) {Car car = new Car(c,licence,t);cars.add(car);return car;}public Customer getCustomer(String name) {for(Customer cust : customers)if(cust.getName().equals(name))return cust;return null;}public double addCharge(ParkingCharge p) {charges.add(p);return p.amount;} public String[] getCustomerIds(){String[] stringArray1 = new String[2];for(int…arrow_forward
- JAVA programming I have to finish the "remove" method which is supposed to remove element e at index ind and returns it. Ill paste the entire code below, but i only have to complete the "remove" method. CODE: package com.mac286.arrays; public class ourArray { //define an array of integers private int[] Ar; //Ar is a reference //declare private int Capacity = 100; private int size = 0; private int Increment = 10; //Default Constructor public ourArray() { Ar = new int[100]; Capacity = 100; size = 0; Increment = 10; } //Constructor that accepts the capacity (C). It creates an array of C integersm, sets capacity to C // Increment to 10 public ourArray(int C) { //create an array of C integers Ar = new int[C]; size = 0; Capacity = C; Increment = 10; } //Constructor that accepts two integers (c, i) c for capacity and i for increment public ourArray(int C, int I) { //create an array of C integers Ar = new int[C]; size = 0; Capacity = C; Increment = I; } //setter for Increment…arrow_forwardAdd the method below to the parking office.java class. Method getParkingCharges(Customer) : Money Parkingoffice.java public class ParkingOffice {String name;String address;String phone; List<Customer> customers;List<Car> cars;List<ParkingLot> lots;List<ParkingCharge> charges; public ParkingOffice(){customers = new ArrayList<>();cars = new ArrayList<>();lots = new ArrayList<>();charges = new ArrayList<>();}public Customer register() {Customer cust = new Customer(name,address,phone);customers.add(cust);return cust;}public Car register(Customer c,String licence, CarType t) {Car car = new Car(c,licence,t);cars.add(car);return car;}public Customer getCustomer(String name) {for(Customer cust : customers)if(cust.getName().equals(name))return cust;return null;}public double addCharge(ParkingCharge p) {charges.add(p);return p.amount;} public String[] getCustomerIds(){String[] stringArray1 = new String[2];for(int i=0;i<2;i++){stringArray1[i]…arrow_forwardQuestion 2. package sortsearchassigncodeex1; import java.util.Scanner; import java.io.*; // // public class Sortsearchassigncodeex1 { // public static void fillArray(Scanner inputFile, int[] arrIn){ int indx = 0; //Complete code { arrIn[indx] = inputFile.nextInt(); indx++; } }arrow_forward
- JAVA programming language i need to create an "addFront" method in the bottom of the code that adds an element at the front. [-1, -3, -5] A.add(-9) ===> [-9, -1, -3, -5]. Its all the way in the bottom, please help me. public class ourArray { //define an array of integers private int[] Ar; //Ar is a reference //declare private int capacity = 100; private int size = 0; private int increment = 10; //default constructor public ourArray() { Ar = new int[100]; capacity = 100; size = 0; increment = 10; } //Constructor that accepts the capacity (C). It creates an array of C integersm, sets capacity to C // Increment to 10 public ourArray(int C) { Ar = new int [C]; size = 0; capacity = C; increment = 10; } //Constructor that accepts two integers (c, i) c for capacity and i for increment public ourArray (int C, int I) { Ar = new int[C]; size = 0; capacity = C; increment = I; } //setter for Increment public void setincrement(int I) { increment = I; } //getter for size, capacity…arrow_forwardimport java.util.HashSet; import java.util.Set; // Define a class named LinearSearchSet public class LinearSearchSet { // Define a method named linearSearch that takes in a Set and an integer target // as parameters public static boolean linearSearch(Set<Integer> set, int target) { // Iterate over all elements in the Set for () { // Check if the current value is equal to the target if () { // If so, return true } } // If the target was not found, return false } // Define the main method public static void main(String[] args) { // Create a HashSet of integers and populate integer values Set<Integer> numbers = new HashSet<>(); // Define the target to search for numbers.add(3); numbers.add(6); numbers.add(2); numbers.add(9); numbers.add(11); // Call the linearSearch method with the set…arrow_forwardThe two classes you will create will implement the operations defined in the interface as shown in the UML class diagram above. In addition, BinarySearchArray will implement a static method testBinarySearchArray() that populates the lists, and lets the user interactively test the two classes by adding and removing elements. The parameter BinarySearch can represent either class and tells testBinarySearchArray which class to test. Steps to Implement: 1) To get started create a new project in IntelliJ called BinarySearch. Add a class to your project called BinarySearchArray. Then add another class BinarySearchArrayList and an interface called BinarySearch. The interface BinarySearch includes public method stubs as shown in the diagram. You are allowed to add BinarySearchArrayList to the same file as BinarySearch but don't add an access modifier to this class, or for easier reading, you can declare the classes in separate files with public access modifiers. Only the class…arrow_forward
- Q1: Write the Java code corresponding to the following UML diagram. The class School implements the interface Building. The method computeArea returns the area of the building as width * length. After that, add a testing class with a main method which defines an arrayList that contains three objects. From the main method, print the area of all the three objects. Building > + computeArea() : double + toString() : String School classroomNum: int width: double - length: double + School(classroomNum: int, width: double, length: double) + computeArea() : double + toString() : Stringarrow_forwardpublic class PokerAnalysis implements PokerAnalyzer { privateList<Card>cards; privateint[]rankCounts; privateint[]suitCounts; /** * The constructor has been partially implemented for you. cards is the * ArrayList where you'll be adding all the cards you're given. In addition, * there are two arrays. You don't necessarily need to use them, but using them * will be extremely helpful. * * The rankCounts array is of the same length as the number of Ranks. At * position i of the array, keep a count of the number of cards whose * rank.ordinal() equals i. Repeat the same with Suits for suitCounts. For * example, if your Cards are (Clubs 4, Clubs 10, Spades 2), your suitCounts * array would be {2, 0, 0, 1}. * * @param cards * the list of cards to be added */ publicPokerAnalysis(List<Card>cards){ this.cards=newArrayList<Card>(); this.rankCounts=newint[Rank.values().length]; this.suitCounts=newint[Suit.values().length];…arrow_forwardComplete the Kennel class by implementing the following methods: addDog(Dog dog) findYoungestDog() method, which returns the Dog object with the lowest age in the kennel. Assume that no two dogs have the same age. Given classes: Class LabProgram contains the main method for testing the program. Class Kennel represents a kennel, which contains an array of Dog objects as a dog list. (Type your code in here.) Class Dog represents a dog, which has three fields: name, breed, and age. (Hint: getAge() returns a dog's age.) For testing purposes, different dog values will be used. Ex. For the following dogs: Rex Labrador 3.5 Fido Healer 2.0 Snoopy Beagle 3.2 Benji Spaniel 3.9 the output is: Youngest Dog: Fido (Healer) (Age: 2.0)arrow_forward
- Computer Networking: A Top-Down Approach (7th Edi...Computer EngineeringISBN:9780133594140Author:James Kurose, Keith RossPublisher:PEARSONComputer Organization and Design MIPS Edition, Fi...Computer EngineeringISBN:9780124077263Author:David A. Patterson, John L. HennessyPublisher:Elsevier ScienceNetwork+ Guide to Networks (MindTap Course List)Computer EngineeringISBN:9781337569330Author:Jill West, Tamara Dean, Jean AndrewsPublisher:Cengage Learning
- Concepts of Database ManagementComputer EngineeringISBN:9781337093422Author:Joy L. Starks, Philip J. Pratt, Mary Z. LastPublisher:Cengage LearningPrelude to ProgrammingComputer EngineeringISBN:9780133750423Author:VENIT, StewartPublisher:Pearson EducationSc Business Data Communications and Networking, T...Computer EngineeringISBN:9781119368830Author:FITZGERALDPublisher:WILEY