Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please add an abstract class and interface to this java program . Main.java //Im

ID: 3721220 • Letter: P

Question

Please add an abstract class and interface to this java program .

Main.java

//Imports:
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

//Application Class
public class Main extends Application {

Stage window;
TableView<Product> table;
TextField nameInput, priceInput, quantityInput;

public static void main(String[] args) {
launch(args);
}

@Override
public void start(Stage primaryStage) throws Exception {
window = primaryStage;
window.setTitle("JoeyWhitmore - JavaFX");

//Name column
TableColumn<Product, String> nameColumn = new TableColumn<>("Name");
nameColumn.setMinWidth(200);
nameColumn.setCellValueFactory(new PropertyValueFactory<>("name"));

//Price column
TableColumn<Product, Double> priceColumn = new TableColumn<>("Price");
priceColumn.setMinWidth(100);
priceColumn.setCellValueFactory(new PropertyValueFactory<>("price"));

//Quantity column
TableColumn<Product, String> quantityColumn = new TableColumn<>("Quantity");
quantityColumn.setMinWidth(100);
quantityColumn.setCellValueFactory(new PropertyValueFactory<>("quantity"));

//Name input
nameInput = new TextField();
nameInput.setPromptText("Name");
nameInput.setMinWidth(100);

//Price input
priceInput = new TextField();
priceInput.setPromptText("Price");

//Quantity input
quantityInput = new TextField();
quantityInput.setPromptText("Quantity");

//Button
Button addButton = new Button("Add");
addButton.setOnAction(e -> addButtonClicked());
Button deleteButton = new Button("Delete");
deleteButton.setOnAction(e -> deleteButtonClicked());

HBox hBox = new HBox();
hBox.setPadding(new Insets(10,10,10,10));
hBox.setSpacing(10);
hBox.getChildren().addAll(nameInput, priceInput, quantityInput, addButton, deleteButton);

table = new TableView<>();
table.setItems(getProduct());
table.getColumns().addAll(nameColumn, priceColumn, quantityColumn);

VBox vBox = new VBox();
vBox.getChildren().addAll(table, hBox);

Scene scene = new Scene(vBox);
window.setScene(scene);
window.show();
}

//Add button clicked
public void addButtonClicked(){
Product product = new Product();
product.setName(nameInput.getText());
product.setPrice(Double.parseDouble(priceInput.getText()));
product.setQuantity(Integer.parseInt(quantityInput.getText()));
table.getItems().add(product);
nameInput.clear();
priceInput.clear();
quantityInput.clear();
}

//Delete button clicked
public void deleteButtonClicked(){
ObservableList<Product> productSelected, allProducts;
allProducts = table.getItems();
productSelected = table.getSelectionModel().getSelectedItems();

productSelected.forEach(allProducts::remove);
}

//Get all of the products
public ObservableList<Product> getProduct(){
ObservableList<Product> products = FXCollections.observableArrayList();
products.add(new Product("Tires", 120, 4));
products.add(new Product("Gum", 1.50, 15));
products.add(new Product("Bricks", 1.99, 250));
products.add(new Product("Laptop", 999.99, 1));
products.add(new Product("Pizza", 8.99, 8));
return products;
}


}

Product.java

public class Product {

private String name;
private double price;
private int quantity;

public Product(){
this.name = "";
this.price = 0;
this.quantity = 0;
}

public Product(String name, double price, int quantity){
this.name = name;
this.price = price;
this.quantity = quantity;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public double getPrice() {
return price;
}

public void setPrice(double price) {
this.price = price;
}

public int getQuantity() {
return quantity;
}

public void setQuantity(int quantity) {
this.quantity = quantity;
}

}

Explanation / Answer

Since the main class extended the Application class we can take the Application class as abstract class.

If we write interface then it will be, public class Main implements Application{}.

// Absract Class

public abstract class Application {

// have to mention abstract for abstract methods.we can write other methods also without

//abstract key word and default implementation.All methods needn't be implemented.

abstract public void addButtonClicked();

abstract public void deleteButtonClicked();

abstract public ObservableList<Product> getProduct();

abstract public void start(Stage primaryStage) throws Exception;

}

// Interface

interface Applicationl{

//abstract methods only.All methods must be implemented by the classes implementing this.

public void addButtonClicked();

public void deleteButtonClicked();

public ObservableList<Product> getProduct();

public void start(Stage primaryStage) throws Exception;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote