Anzeige

can you add a delete button and a add button to the below program. j.pdf

sales88
1. Apr 2023
can you add a delete button and a add button to the below program. j.pdf
can you add a delete button and a add button to the below program. j.pdf
can you add a delete button and a add button to the below program. j.pdf
can you add a delete button and a add button to the below program. j.pdf
Anzeige
can you add a delete button and a add button to the below program. j.pdf
can you add a delete button and a add button to the below program. j.pdf
can you add a delete button and a add button to the below program. j.pdf
can you add a delete button and a add button to the below program. j.pdf
Nächste SlideShare
TypeScript IntroductionTypeScript Introduction
Wird geladen in ... 3
1 von 8
Anzeige

Más contenido relacionado

Más de sales88(20)

Anzeige

can you add a delete button and a add button to the below program. j.pdf

  1. can you add a delete button and a add button to the below program. java fx package nusoft; public class Car { private String make; private String model; private int year; private String color; public Car(String make, int year, String model, String color) { this.make = make; this.model = model; this.year = year; this.color = color; } public String getMake() { return make; } public void setMake(String make) { this.make = make; } public String getModel() { return model; } public void setModel(String model) { this.model = model; } public int getYear() { return year; }
  2. public void setYear(int year) { this.year = year; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } @Override public String toString() { return year + " " + make + " " + model + " (" + color + ")"; } } package nusoft; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import javafx.application.Application; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.layout.BorderPane; import javafx.scene.layout.VBox; import javafx.stage.Stage; public class CarNavigator extends Application { private ArrayList cars = new ArrayList<>(); private int currentIndex = 0; @Override
  3. public void start(Stage primaryStage) throws Exception { readCarsFile(); Label carLabel = new Label(); carLabel.setAlignment(Pos.CENTER); updateCarLabel(carLabel); Button prevButton = new Button("Previous"); prevButton.setOnAction(e -> { currentIndex--; if (currentIndex < 0) { currentIndex = cars.size() - 1; } updateCarLabel(carLabel); }); Button nextButton = new Button("Next"); nextButton.setOnAction(e -> { currentIndex++; if (currentIndex >= cars.size()) { currentIndex = 0; } updateCarLabel(carLabel); }); VBox buttonBox = new VBox(10, prevButton, nextButton); buttonBox.setAlignment(Pos.CENTER); BorderPane root = new BorderPane(carLabel, null, null, buttonBox, null); Scene scene = new Scene(root, 400, 200); primaryStage.setScene(scene); primaryStage.show(); } private void readCarsFile() throws IOException {
  4. BufferedReader reader = new BufferedReader(new FileReader("cars.txt")); String line; while ((line = reader.readLine()) != null) { cars.add(line); } reader.close(); } private void updateCarLabel(Label label) { label.setText(cars.get(currentIndex)); } public static void main(String[] args) { launch(args); } } package nusoft.utils; import java.util.NoSuchElementException; import nusoft.Car; public class NuLinkedList { private Node head; private Node tail; private int size; private static class Node { E element; Node prev; Node next; Node(E element, Node prev, Node next) { this.element = element; this.prev = prev; this.next = next; } } public boolean contains(E e) { return indexOf(e) != -1; }
  5. public E get(int index) { if (index < 0 || index >= size) { throw new IndexOutOfBoundsException(); } return getNode(index).element; } public int indexOf(E e) { int index = 0; for (Node node = head; node != null; node = node.next) { if (e == null ? node.element == null : e.equals(node.element)) { return index; } index++; } return -1; } public int lastIndexOf(E e) { int index = size - 1; for (Node node = tail; node != null; node = node.prev) { if (e == null ? node.element == null : e.equals(node.element)) { return index; } index--; } return -1; } public E set(int index, E e) { if (index < 0 || index >= size) { throw new IndexOutOfBoundsException(); } Node node = getNode(index); E oldElement = node.element; node.element = e; return oldElement; } public void add(E e) {
  6. addLast(e); } public void add(int index, E e) { if (index < 0 || index > size) { throw new IndexOutOfBoundsException(); } if (index == size) { addLast(e); } else { addBefore(getNode(index), e); } } public void addFirst(E e) { if (size == 0) { head = tail = new Node<>(e, null, null); } else { head.prev = new Node<>(e, null, head); head = head.prev; } size++; } public void addLast(E e) { if (size == 0) { head = tail = new Node<>(e, null, null); } else { tail.next = new Node<>(e, tail, null); tail = tail.next; } size++; } public E remove(int index) { if (index < 0 || index >= size) { throw new IndexOutOfBoundsException(); } return remove(getNode(index)); }
  7. public E removeFirst() { if (size == 0) { throw new NoSuchElementException(); } return remove(head); } public E removeLast() { if (size == 0) { throw new NoSuchElementException(); } return remove(tail); } public int size() { return size; } private Node getNode(int index) { if (index < size / 2) { Node node = head; for (int i = 0;i < index; i++) { node = node.next; } return node; } else { Node node = tail; for (int i = size - 1; i > index; i--) { node = node.prev; } return node; } } private void addBefore(Node node, E e) { Node newNode = new Node<>(e, node.prev, node); node.prev.next = newNode; node.prev = newNode; size++; }
  8. private E remove(Node node) { if (node == head) { head = node.next; } else { node.prev.next = node.next; } if (node == tail) { tail = node.prev; } else { node.next.prev = node.prev; } size--; return node.element; } @Override public String toString() { StringBuilder sb = new StringBuilder("["); for (Node node = head; node != null; node = node.next) { if (node != head) { sb.append(", "); } sb.append(node.element); } sb.append("]"); return sb.toString(); } public void remove(Car lastCar) { // TODO Auto-generated method stub } } ( import java. io. BufferedReader;
Anzeige