Can't compile Oracle docs example - java

this is my first question here so bear with me please.
I'm trying to use the Oracle Java docs, but when I try to compile example 6 on this page it doesn't work. I've tried everything I can think of. It's an old example, so that may be the issue, but it's disheartening that I can't figure out what should be a minor problem.
Example 6 Using an InvalidationListener
package bindingdemo;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.binding.NumberBinding;
import javafx.beans.binding.Bindings;
import javafx.beans.InvalidationListener;
import javafx.beans.Observable;
class Bill {
// Define the property
private DoubleProperty amountDue = new SimpleDoubleProperty();
// Define a getter for the property's value
public final double getAmountDue(){return amountDue.get();}
// Define a setter for the property's value
public final void setAmountDue(double value){amountDue.set(value);}
// Define a getter for the property itself
public DoubleProperty amountDueProperty() {return amountDue;}
}
public class Main {
public static void main(String[] args) {
Bill bill1 = new Bill();
Bill bill2 = new Bill();
Bill bill3 = new Bill();
NumberBinding total = Bindings.add(bill1.amountDueProperty().add(bill2.amountDueProperty()),
bill3.amountDueProperty());
total.addListener(new InvalidationListener() {
#Override public void invalidated(Observable o) {
System.out.println("The binding is now invalid.");
}
});
// First call makes the binding invalid
bill1.setAmountDue(200.00);
// The binding is now invalid
bill2.setAmountDue(100.00);
bill3.setAmountDue(75.00);
// Make the binding valid...
System.out.println(total.getValue());
// Make invalid...
bill3.setAmountDue(150.00);
// Make valid...
System.out.println(total.getValue());
}
}
Thanks

Related

How do I access an ArrayList written in one class from another class?

So I'm doing a Java project for college where I have to simulate a restaurant. The two classes that are giving me trouble are Menu and Bill. I have written four ArrayLists in Menu for all possible starters, main course, desserts and drinks. I need to be able to access these ArrayLists in Bill to generate the bill / receipt, but I'm having trouble.
The Menu class:
import Foods.Desserts;
import Foods.Drinks;
import Foods.Main;
import Foods.Starter;
import java.util.ArrayList;
public class Menu {
/**
* #author Max Huddlestan
*/
//Created Array lists for each course to track the prices
ArrayList<Starter> starters;
ArrayList<Main> mains;
ArrayList<Desserts> desserts;
ArrayList<Drinks> drinks;
//this constructor should make it so each class had each of these array lists
public Menu(){
addStarters();
addMain();
addDesserts();
addDrinks();
}
//using an object of each class i can add a name and a price to the food in its respective course
public void addStarters(){
starters = new ArrayList<Starter>();
starters.add(new Starter("Soup", 8.00));
starters.add(new Starter("Garlic Bread", 8.00));
starters.add(new Starter("Chicken Wings", 9.00));
starters.add(new Starter("Caesar Salad", 10));
starters.add(new Starter("N/A", 0));
}
public void addMain(){
mains = new ArrayList<Main>();
mains.add(new Main ("Beef Burger", 16.5));
mains.add(new Main("Steak", 18.50));
mains.add(new Main("Spaghetti Bolognese", 14.00));
mains.add(new Main("Pizza", 14.75));
mains.add(new Main("Vegan Lasagne", 15.30));
mains.add(new Main("N/A", 0));
}
public void addDesserts(){
desserts = new ArrayList<Desserts>();
desserts.add(new Desserts("Sticky Toffee Pudding", 7.5));
desserts.add(new Desserts("Vegan Brownie", 7.5));
desserts.add(new Desserts("Ice Cream Sundae", 7.5));
desserts.add(new Desserts("Apple Tart", 7.5));
desserts.add(new Desserts("N/A", 0));
}
public void addDrinks() {
drinks = new ArrayList<Drinks>();
drinks.add(new Drinks("Beer", 5.3));
drinks.add(new Drinks("Wine", 7.0));
drinks.add(new Drinks("Coca Cola", 3.30));
drinks.add(new Drinks("Fanta", 3.30));
drinks.add(new Drinks("Water", 0));
drinks.add(new Drinks("N/A", 0));
}
public ArrayList<Starter> getStarters() {return starters;}
public ArrayList<Main> getMains() {return mains;}
public ArrayList<Desserts> getDesserts() {return desserts;}
public ArrayList<Drinks> getDrinks() {return drinks;}
#Override
public String toString() {
String startersList = "+";
for (Starter s : starters) {
startersList += s.toString();
}
return startersList;
}
}
The Bill class:
package BillsIncome;
import Foods.Desserts;
import Foods.Drinks;
import Foods.Main;
import Foods.Starter;
import java.awt.*;
import java.util.ArrayList;
public class Bill {
public static void main(String[] args) {
Menu menu = new Menu();
TakeOrder orders = new TakeOrder();
ArrayList<String> order = new ArrayList<String>();
order.add(orders.selectStarter());
order.add(orders.selectMain());
order.add(orders.selectDessert());
order.add(orders.selectDrink());
System.out.println(menu.getStarters());
}
}
The line
System.out.println(menu.getStarters());
Is giving me an error: "Cannot resolve method getStarters in Menu"
Help?? :P
Basically, like #tgdavies said, your problem is that your program assumes the Menu object you're trying to create is from java.awt.Menu.
To get your main method to work, all you'll have to do is create a package for your Menu.java, with something like this as the header:
package Restaurant;
import java.util.ArrayList;
public class Menu {
/**
* #author Max Huddlestan
*/
// ... All other class stuff below
}
Now your Menu class will be associated with the Restaurant package, which you can import in your Bill program by doing import Restaurant.Menu; at the top of the program.
If you don't want to have to import your Menu class in the Bill program, you need to make sure the two files are in the same directory.
I hope this helps clarify things!

JavaFX ChoiceBox - How can you update the text of the popup items?

I have a ChoiceBox where I can select the language for my program. When I select another language, the label gets translated as desired (because it is recomputed using ChoiceBoxSkin#getDisplayText and my StringConverter takes the language into account), but the elements in the popup list stay the same.
Now, I could do something like
public void updateStrings() {
var converter = getConverter();
setConverter(null);
setConverter(converter);
var selected = valueProperty().getValue();
valueProperty().setValue(null);
valueProperty().setValue(selected);
}
in my ChoiceBox-subclass. This will re-populate the popup list with the correctly translated texts. Setting the value again is necessary beacause ChoiceBoxSkin#updatePopupItems (which is triggered when changing the converter) also resets the toggleGroup. That means that the selected item would no longer be marked as selected in the popup list.
Despite being kind of ugly, this actually works for my current use case. However, it breaks if any listener of the valueProperty does something problematic on either setting it to null or selecting the desired item a second time.
Am I missing a cleaner or just all-around better way to achieve this?
Another approach might be to use a custom ChoiceBoxSkin. Extending that, I'd have access to ChoiceBoxSkin#getChoiceBoxPopup (although that is commented with "Test only purpose") and could actually bind the text properties of the RadioMenuItems to the corresponding translated StringProperty. But that breaks as soon as ChoiceBoxSkin#updatePopupItems is triggered from anywhere else...
A MRP should be:
import javafx.scene.control.ChoiceBox;
import javafx.util.StringConverter;
public class LabelChangeChoiceBox extends ChoiceBox<String> {
private boolean duringUpdate = false;
public LabelChangeChoiceBox() {
getItems().addAll("A", "B", "C");
setConverter(new StringConverter<>() {
#Override
public String toString(String item) {
return item + " selected:" + valueProperty().getValue();
}
#Override
public String fromString(String unused) {
throw new UnsupportedOperationException();
}
});
valueProperty().addListener((observable, oldValue, newValue) -> {
if(duringUpdate) {
return;
}
duringUpdate = true;
updateStrings();
duringUpdate = false;
});
}
public void updateStrings() {
var converter = getConverter();
setConverter(null);
setConverter(converter);
var selected = valueProperty().getValue();
valueProperty().setValue(null);
valueProperty().setValue(selected);
}
}
And an Application-class like
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import ui.LabelChangeChoiceBox;
public class Launcher extends Application {
#Override
public void start(Stage stage) {
Scene scene = new Scene(new LabelChangeChoiceBox());
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch();
}
}
This works but needs the duringUpdate variable and can break if there is another change listener.
I’m not sure if this meets your needs, as your description of the problem is unclear in a few places.
Here’s a ChoiceBox which updates its converter using its own chosen language, and also retains its value when that change occurs:
import java.util.Locale;
import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.geometry.Insets;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.ChoiceBox;
import javafx.scene.layout.BorderPane;
import javafx.util.StringConverter;
public class FXLocaleSelector
extends Application {
#Override
public void start(Stage stage) {
ChoiceBox<Locale> choiceBox = new ChoiceBox<>();
choiceBox.getItems().addAll(
Locale.ENGLISH,
Locale.FRENCH,
Locale.GERMAN,
Locale.ITALIAN,
Locale.CHINESE,
Locale.JAPANESE,
Locale.KOREAN
);
choiceBox.converterProperty().bind(
Bindings.createObjectBinding(
() -> createConverter(choiceBox.getValue()),
choiceBox.valueProperty()));
BorderPane pane = new BorderPane(choiceBox);
pane.setPadding(new Insets(40));
stage.setScene(new Scene(pane));
stage.setTitle("Locale Selector");
stage.show();
}
private StringConverter<Locale> createConverter(Locale locale) {
Locale conversionLocale =
(locale != null ? locale : Locale.getDefault());
return new StringConverter<Locale>() {
#Override
public String toString(Locale value) {
if (value != null) {
return value.getDisplayName(conversionLocale);
} else {
return "";
}
}
#Override
public Locale fromString(String s) {
return null;
}
};
}
public static void main(String[] args) {
launch(FXLocaleSelector.class, args);
}
}
Not entirely certain whether or not I understand your requirement correctly, my assumptions:
there's a ChoiceBox which contains the "language" for your ui, including the itself: lets say it contains the items Locale.ENGLISH and Locale.GERMAN, the visual representation of its items should be "English", "German" if its value is Locale.ENGLISH and "Englisch", "Deutsch" if its value is Locale.GERMAN
the visual representation is done by a StringConverter configurable with the value
If so, the solution is in separating out concerns - actually, it's not: the problem described (and hacked!) in the question is JDK-8088507: setting the converter doesn't update the selection of the menu items in the drop down. One hack is as bad or good as another, my personal preferenced would go for a custom skin which
adds a change listener to the converter property
reflectively calls updateSelection
Something like:
public static class MyChoiceBoxSkin<T> extends ChoiceBoxSkin<T> {
public MyChoiceBoxSkin(ChoiceBox<T> control) {
super(control);
registerChangeListener(control.converterProperty(), e -> {
// my local reflection helper, use your own
FXUtils.invokeMethod(ChoiceBoxSkin.class, this, "updateSelection");
});
}
}
Note: the hacks - this nor the OP's solution - do not solve the missing offset of the popup on first opening (initially or after selecting an item in the popup).
Not a solution to the question, just one way to have a value-dependent converter ;)
have a StringConverter with a fixed value (for simplicity) for conversion
have a converter controller having that a property with that value and a second property with a converter configured with the value: make sure the converter is replaced on change of the value
bind the controller's value to the box' value and the box' converter to the controller's converter
In (very raw) code:
public static class LanguageConverter<T> extends StringConverter<T> {
private T currentLanguage;
public LanguageConverter(T language) {
currentLanguage = language;
}
#Override
public String toString(T object) {
Object value = currentLanguage;
return "" + object + (value != null ? value : "");
}
#Override
public T fromString(String string) {
return null;
}
}
public static class LanguageController<T> {
private ObjectProperty<StringConverter<T>> currentConverter = new SimpleObjectProperty<>();
private ObjectProperty<T> currentValue = new SimpleObjectProperty<>() {
#Override
protected void invalidated() {
currentConverter.set(new LanguageConverter<>(get()));
}
};
}
Usage:
ChoiceBox<String> box = new ChoiceBox<>();
box.getItems().addAll("A", "B", "C");
box.getSelectionModel().selectFirst();
LanguageController<String> controller = new LanguageController<>();
controller.currentValue.bind(box.valueProperty());
box.converterProperty().bind(controller.currentConverter);

Cannot instantiate ObservableList with an extractor

I have a custom object FermentableInRecipe, which populates a TableView. In order to respond to changes to items in the list, as well as the list itself, I have decided to employ an extractor. Here is my declaration and instantiation of my ObservableList:
private ObservableList<FermentableInRecipe> fermentablesInRecipe =
FXCollections.observableArrayList(item -> new Observable[]{item.WeightProperty()});
Here are the relevant segments of my custom class:
public class FermentableInRecipe {
private DoubleProperty weight;
...
public Double getWeight() {
return this.weight.getValue();
}
public void setWeight(Double value) {
this.weight.setValue(value);
}
public DoubleProperty WeightProperty() {
if (weight == null) {
weight = new SimpleDoubleProperty(0.0);
}
return weight;
}
...
}
In the links I've provided below, this approach worked. But Netbeans is telling me "DoubleProperty cannot be converted to Observable". I can see why this is the case, but I cannot understand why it worked in the links below and not for me, and how I should create extractor and link it to the weightProperty() function if this approach doesn't work.
Links:
JavaFX 2.0 Choice Box Issue. How to update a choiceBox, which represents a list of objects, when an object is updated?
JavaFX, ObservableList: How to fire an InvalidationListener whenever an object of the list gets modified?
Thanks in advance. Let me know if I've missed any crucial information.
There's nothing wrong with your code as written, this compiles just fine for me:
import javafx.application.Application;
import javafx.beans.Observable;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.stage.Stage;
public class JavaFXApplication1 extends Application {
class FermentableInRecipe {
private DoubleProperty weight;
public Double getWeight() {
return this.weight.getValue();
}
public void setWeight(Double value) {
this.weight.setValue(value);
}
public DoubleProperty WeightProperty() {
if (weight == null) {
weight = new SimpleDoubleProperty(0.0);
}
return weight;
}
}
private ObservableList<FermentableInRecipe> fermentablesInRecipe = FXCollections.observableArrayList(item -> new Observable[]{item.WeightProperty()});
#Override
public void start(Stage primaryStage) throws Exception {
}
}
I'd suggest double checking imports, and make sure you haven't imported java.util.Observable or similar by mistake.

Deeplearning on Spark pipeline: How to predict using a neural network model in a pipeline?

I am trying to add sentiment analysis program to Spark pipeline. When doing it, I have class which extends org.apache.spark.ml.PredictionModel. When extending this PredictionModel class, I have to override predict() method which predicts the label for given feature. But, I get either 0 or 1 all the time when I execute this code.For example, if there are 10 movie reviews, five are negative reviews and other five are negative, it classifies all reviews as negative. I have attached the code below.
import org.apache.spark.ml.PredictionModel;
import org.apache.spark.ml.param.ParamMap;
import org.apache.spark.mllib.linalg.DenseVector;
import org.apache.spark.mllib.linalg.Vector;
import org.deeplearning4j.nn.multilayer.MultiLayerNetwork;
import org.nd4j.linalg.api.buffer.DataBuffer;
import org.nd4j.linalg.api.ndarray.INDArray;
import org.nd4j.linalg.factory.Nd4j;
import java.io.*;
//Model produced by a ProbabilisticClassifier
public class MovieReviewClassifierModel extends PredictionModel<Object, MovieReviewClassifierModel> implements Serializable{
private static final long serialVersionUID = 1L;
private MultiLayerNetwork net;
MovieReviewClassifierModel (MultiLayerNetwork net) throws Exception {
this.net=net;
}
#Override
public MovieReviewClassifierModel copy(ParamMap args0) {
return null;
}
#Override
public String uid() {
return "MovieReviewClassifierModel";
}
public double raw2prediction(Vector rawPrediction) {//Given a vector of raw predictions, select the predicted label
return rawPrediction.toArray()[0];
}
#Override
public double predict(Object o) {
int prediction=0;
DenseVector v=(DenseVector)o;
double[] a=v.toArray();
INDArray arr=Nd4j.create(a);
INDArray array= net.output(arr,false);
DataBuffer ob = array.data();
double[] d=ob.asDouble();
double zeroProbability=d[0];
double oneProbability=d[1];
if (zeroProbability > oneProbability) {
prediction=0;
}
else{
prediction=1;
}
return prediction;
}
}
Can you give me reasons for the wrong predictions?
In public double predict(Object o) you have a following if statement:
if (zeroProbability > oneProbability) {
prediction=0;
}
else{
prediction=1;
}
which causes the return of 0 or 1. Change this method in order to have some other prediction values.

Access another class's data member's field in Java

So, I want to access one class's data member's field from a whole other class through reflection. I have been unable to figure out, how, after I get the data member through reflection, I can change the field's value. I don't really know how to express it better, so I will let the code speak for me.
Here follows the handler class that calls the buttons. Following are the rest of the classes, whose functionality I will explain on the go.
import java.awt.*;
import java.awt.event.*;
public class SimHandler extends Frame{
public myValveButton but0,but1,but2,but3,but4,but5,but6,but7;
public SimHandler(){
super("Liquer Plant Control Panel");
this.setLayout(null);
this.setFont(new Font("Helvetica", Font.PLAIN, 14));
this.setBackground(Color.black);
but0 = new myValveButton("S1a",100,40,this);
but1 = new myValveButton("S1b",100,140,this);
but2 = new myValveButton("S2a",200,40,this);
but3 = new myValveButton("S2b",200,140,this);
but4 = new myValveButton("S3a",100,240,this);
but5 = new myValveButton("S3b",100,340,this);
but6 = new myValveButton("S4a",200,240,this);
but7 = new myValveButton("S4b",200,340,this);
this.setSize(335,410);
this.setLocation(100,100);
this.setVisible(true);
this.toFront();
this.setResizable(false);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
This is actually where I try to use reflection to change the value of Silo's instance state. Below this one follow LiqPlantSim and Silo classes. As you can see, the state variable cannot be resolved that way, and after quite some googling, I can't figure out how I can make it work.
import java.awt.Button;
import java.awt.Frame;
import java.awt.event.*;
import java.lang.reflect.Field;
public class myValveButton extends Button{
String label;
public myValveButton(String label,int x,int y,Frame f){
super(label);
this.label = label;
this.addActionListener(new myValveButtonHandler(label));
f.add(this);
this.setBounds(x, y, 35, 30);
}
}
class myValveButtonHandler implements ActionListener{
Field f;
String label;
public myValveButtonHandler(String label){
this.label = label;
}
public void actionPerformed(ActionEvent pushButton){
try {
f = LiqPlantSim.class.getDeclaredField("silo"+label.split("")[1]);
System.out.println(f);
//f.state = "full" //Eclipse says 'state cannot be resolved to a type or is not a field'
} catch (NoSuchFieldException e) {
} catch (SecurityException e) {
}
System.out.println(label.split("")[2]);
}
}
Here is the LiqPlantSim class.
import java.util.HashMap;
import java.util.Map;
public class LiqPlantSim{
public Silo silo1,silo2,silo3,silo4;
public Pipe pipe;
public LiqPlantSim(){
silo1 = new Silo(false,false);
silo2 = new Silo(false,true);
silo3 = new Silo(true,false);
silo4 = new Silo(true,true);
pipe = new Pipe();
}
}
Here is the Silo Class.
public class Silo {
public boolean mixer,resistance;
public String state,mixerState,resState;
public Silo(boolean mix,boolean res){
mixer = mix;
resistance = res;
state = "empty";
}
}
Apart from finding out how I can access the silo's state variables, I would really appreciate any feedback and/or advice on how I could structure my work better, and on any mistakes I might have made.
First off, Class#getDeclaredField(String) returns a Field object, not the actual value of that field. To get the value, you must use Field#get(Object), where the argument is an instance of the class for which you are trying to access a field. In your code, this would be:
LiqPlantSim sim = doSomethingToGetInstance();
f = LiqPlantSim.class.getDeclaredField("siloX");
Silo silo = (Silo) f.get(sim);
Which brings me to my next point: Why use reflection at all? Your answer probably has to do with getting the correct Silo using the label. You should* restructure LiqPlantSim to use an array or a List to solve this:
import java.util.HashMap;
import java.util.Map;
import java.util.List;
import java.util.ArrayList;
public class LiqPlantSim{
private List<Silo> silos;
public Pipe pipe;
public LiqPlantSim(){
silos = new ArrayList<>();
silos.add(new Silo(false,false));
silos.add(new Silo(false,true));
silos.add(new Silo(true,false));
silos.add(new Silo(true,true));
pipe = new Pipe();
}
public Silo getSilo(int index) {
return silos.get(index);
}
//Possibly other methods to access silos
}
(EDIT: You should do the same with the buttons in SimHandler)
Then, in the handler, you can access a Silo like this:
public void actionPerformed(ActionEvent pushButton){
try {
int labelLength = label.length();
int index = Integer.parseInt(label.substring(labelLength - 1, labelLength));
Silo silo = doSomethingToGetLiqPlantSimInstance().getSilo(index);
silo.state = "full" //Note that it is good practice to use private fields and public getters and setters
} catch (NoSuchFieldException e) {
} catch (SecurityException e) {
}
System.out.println(label.split("")[2]);
}
Better yet, get the index in the constructor and store it so you don't have to recalculate it every time.
*Of course, this is only a suggestion
Why do you implement your action-listeners in the button class? The ValveButton should not be aware of what to do when it is clicked.
Instead you should implement your action-listener in your SimHandler class. After instantiating your 8 ValveButtons you can add the action-listeners in a loop.
Anyways - if you really need to go for a solution using reflection I would recomment using the tiny PrivilegedAccessor framework. Though recommended to be used only in unit-tests it might be useful in your case.
This: "silo"+label.split("")[1] will create the String siloS.
To get the number from the label variable try: label.substring(1,2)
Also you don't need to pass the Frame f to the myValveButton constructor, you can add the buttons to the frame directly in the SimHandler using this.add(but0).

Categories