Novice java coder here, expert java drinker. I've been working on an assignment for my class all day where I have to write a java class known as "Pet" for a fictional program called Pet BAG(Boarding and grooming) for my fictional employer Global Rain. Anyway, I have alot of said code written, and am continuing to work on it dilligently however I have hit a roadblock. When I try the instant feed back tool I get "getDogSpaces()" or "getCatSpaces()" functions seem to be missing. Can anyone help me understand why I get this error so I can fix it and move on? I am stunped. My code is posted below.
public class Pet {
//Atrributes
private String petType;
private String petName;
private int petAge;
private int dogSpaces;
private int catSpaces;
private int daysStay;
private double amountDue;
public Pet (String petType, String petName, int petAge, int daysStay) {
setPetType(petType);
setPetName(petName);
setPetAge(petAge);
setDogSpace(30);
setCatSpace(12);
setDaysStay(daysStay);
}
//Accessors
public String getPetType() {
return petType;
}
public String getPetName() {
return petName;
}
public int getPetAge() {
return petAge;
}
public void setPetAge(int setPetAge) {
this.petAge = petAge;
}
public int getDogSpace() {
return dogSpace;
}
public void setDogSpace(int dogSpace) {
this.dogSpace = dogSpace;
}
public int getCatSpace() {
return catSpace;
}
public void setCatSpace(int catSpace) {
this.catSpace = catSpace;
}
public int getDaysStay() {
return daysStay;
}
public void setDaysStay(int daysStay) {
this.daysStay = daysStay;
}
public double getAmountDue() {
return amountDue;
}
public void setAmountDue(double amountDue) {
this.amountDue = amountDue;
}
}
I know my code block seems strange, but I did my best to line it up properly. Could anyone assist me?
Thank you,
Joseph
Related
I need to make a programm which is like a rally, theres 2 types of vehicles, motorcycle and cars, two types of motorcycle, with and without sidecar, the thing is that I need to verify if there is just a motorcycle in an array list, I mean, two wheels vehicle. That verification should be done in a method called esDe2Ruedas(), which is called by an abstract overrided method called check() that should be the one that verifies if a group of vehicles from an array are able to run in the rally, if its true all the elements of the array must be from the same type.
Here is the code
this is how the program arrays the vehicles
GrandPrix gp1 = new GrandPrix();
gp1.agregar(v1);
//gp1.mostrar(v1);
gp1.agregar(v2);
System.out.println(gp1.check());
GrandPrix gp2 = new GrandPrix();
gp2.agregar(vt1);
gp2.agregar(vt2);
gp2.agregar(m2);
System.out.println(gp2.check());
GrandPrix gp3 = new GrandPrix();
gp3.agregar(vt1);
gp3.agregar(vt2);
gp3.agregar(m1);
System.out.println(gp3.check());
GrandPrix gp4 = new GrandPrix();
gp4.agregar(m1);
gp4.agregar(m2);
System.out.println(gp4.check());
This is the class that is using
import java.util.ArrayList;
public class GrandPrix extends Rally{
ArrayList<Vehiculo> ve = new ArrayList<Vehiculo>();
public void agregar(Vehiculo v) {
ve.add(v);
}
public void agregar(Carro c) {
ve.add(c);
}
public void agregar(Moto m) {
ve.add(m);
}
#Override
boolean check() {// HERE I VERIFY IF THE VEHICLES ARE COMPATIBLE
return false;
}
}
This is the class where everything goes on
public class Vehiculo {
private String Nombre;
private double velocidad_max;
private int peso;
private int comb;
public Vehiculo() {
setNombre("Anónimo");
setVel(130);
setPeso(1000);
setComb(0);
}
public Vehiculo(String string, double d, int i, int j) {
setNombre(string);
setVel(d);
setPeso(i);
setComb(j);
}
double rendimiento() {
return velocidad_max/peso;
}
public boolean mejor(Vehiculo otroVehiculo) {
return rendimiento()>otroVehiculo.rendimiento();
}
public String toString() {
return getNombre()+"-> Velocidad máxima = "+getVel()+" km/h, Peso = "+getPeso()+" kg";
}
/**************************************
---------SET And GET Nombre------------
***************************************/
public String getNombre() {
return Nombre;
}
public void setNombre(String nuevoNombre) {
this.Nombre=nuevoNombre;
}
/**************************************
---------SET And GET velocidad_max------------
***************************************/
public double getVel() {
return velocidad_max;
}
public void setVel(double nuevaVel) {
this.velocidad_max=nuevaVel;
}
/**************************************
---------SET And GET peso------------
***************************************/
public double getPeso() {
return peso;
}
public void setPeso(int nuevoPeso) {
this.peso=nuevoPeso;
}
/**************************************
---------SET And GET comb------------
***************************************/
public int getComb() {
return comb;
}
public void setComb(int comb) {
this.comb = comb;
}
boolean esDe2Ruedas() {
return false;
}
}
This is the class of motorcycles, which is in theory the same as the car's class, without sidecar thing
public class Moto extends Vehiculo{
private boolean sidecar;
public Moto(String string, double d, int i, int j) {
setNombre(string);
setVel(d);
setPeso(i);
setComb(j);
setSidecar(false);
}
public Moto(String string, double d, int i, int j, boolean b) {
setNombre(string);
setVel(d);
setPeso(i);
setComb(j);
setSidecar(b);
esDe2Ruedas(false);
}
public String toString() {
String str = null;
if(isSidecar())
str =super.toString()+", Moto, con sidecar";
else
str =super.toString()+", Moto";
return str;
}
public boolean isSidecar() {
return sidecar;
}
public void setSidecar(boolean sidecar) {
this.sidecar = sidecar;
}
I guess what you presented is what is given. If you came up with the design it is ok, but I believe it could be improved. Anyway, I try to respond to what I believe was your question straight away.
Vehiculo is the super type of Moto (which can have a side car and becomes 3 wheeler).
Vehiculo has a method esDe2Ruedas, which returns false.
Moto inherits that method <-- this is wrong, it should override it and, depending on side car, return the expected boolean value.
In the check method you can now distinguish between Moto and "Moto with sidecar" by using that method.
i am trying to write a class which has an array of a subclass in the same project, and when i am trying to write a method that will add a new object to the array on condition that this object is not already in the array, and also if the specific cell is free, so the object will enter to the array.
but the problem is that i need to insert a variable to this method which is the copy constructor's object.
the problem is that in the subclass i don't know how to write the copy constructor.
so i will give a short example of 2 classes and you will show me how to write a copy constructor with them :
public class Food
{
private String _foodName;
public Food(String foodName)
{
_foodName=foodName;
}
public String getFoodName()
{
return _foodName;
}
public void showName()
{
System.out.println("The food's name is: " +_getFoodName());
}
}
public class Apple extends Food
{
private int _numOfApples;
public Apple(String name, int numOfApples)
{
super(name);
_numOfApples=numOfApples;
}
public Apple(Apple other)
{
????
}
}
how does the copy constructor should looks like ?
thank you for your help :)
By invoking the other constructor. Like,
public Apple(Apple other) {
this(other.getFoodName(), other._numOfApples);
}
I cannot help you properly because i cannot understand entirely what you are trying to do but at least i can help you correct some mistakes in your code:
public class Food
{
private String food;
public Food(String foodName)
{
food = foodName;
}
public String getFoodName()
{
return food;
}
public void showName()
{
System.out.println("The food's name is: " + getFoodName());
}
}
public class Apple extends Food
{
private int numOfApples;
public Apple(String name, int numberOfApples)
{
super(food);
numOfApples=numberOfApples;
}
public Apple(Apple copy)
{
this.name = copy.name;
this.numOfApples = copy.numOfApples;
}
}
Hope this helps.
I am trying to sort an ArrayList in increasing order in reference to a certain variable. This is the problem question.
q5: Create a public class named Snow with private instance variables vast, prior, ethnic, and remarkable each of type int. You may add any other methods and variables you'd like to this class.
Outside of Snow (in the Problem Set class) write a public static method named sortSnow that takes an ArrayList of Snows as a parameter and returns void. This method will sort the input by the variable remarkable in increasing order
This is what I wrote.
public class snow implements Comparable<snow> {
private int vast;
private int prior;
private int ethnic;
private int remarkable;
public snow( int vast , int prior, int ethnic ,int remarkable) {
this.vast=vast;
this.prior = prior;
this.ethnic = ethnic;
this.remarkable = remarkable;
}
public int getEthnic() {
return ethnic;
}
public void setEthnic(int ethnic) {
this.ethnic = ethnic;
}
public int getPrior() {
return prior;
}
public void setPrior(int prior) {
this.prior = prior;
}
public int getVast() {
return vast;
}
public void setVast(int vast) {
this.vast = vast;
}
public int getRemarkable() {
return remarkable;
}
public void setRemarkable(int remarkable) {
this.remarkable = remarkable;
}
public int compareTo(snow compareSnow) {
// TODO Auto-generated method stub
int compareThese = ((snow) compareSnow).getRemarkable();
//ascending order
return this.remarkable - compareThese;
}
}
public static void sortSnow(ArrayList<snow>input){
Collections.sort(input);
}
I am not understanding what the error means. The autolab is giving me this error:
Could not find class submission.ProblemSet$Snow
Java is case sensitive i.e. snow is not Snow is not sNoW. Rename your class to Snow and try again. Also, it is ArrayList and not arraylist.
Then to sort a List, you can use Collections.sort.
I think this is you want to achieve
Save below code in file called "Snow.java" compile it and try to run it.
import java.util.ArrayList;
import java.util.Collections;
//As ".java" file can contain only single public java class
//I made Problem set class non-public so we can use its main method
//to run and see output
class ProblemSet {
public static void main(String[] args) {
Snow one = new Snow(1,1,1,1);
Snow two = new Snow(1,1,1,2);
Snow three = new Snow(1,1,1,3);
Snow four = new Snow(1,1,1,4);
Snow five = new Snow(1,1,1,5);
Snow six = new Snow(1,1,1,6);
ArrayList arrayList = new ArrayList();
arrayList.add(one);
arrayList.add(three);
arrayList.add(five);
arrayList.add(two);
arrayList.add(six);
arrayList.add(four);
System.out.println("Without sort");
System.out.println(arrayList);
sortSnow(arrayList);
System.out.println("With sort");
System.out.println(arrayList);
}
//this is your static method which takes argument as array list of Snow
//And it applies sorting logic based on compareTo method which you wrote
//in Snow class. As per java best practice Class name should start with
//Upper case letters and follow camel casing I renamed your class from
//"snow" to "Snow"
public static void sortSnow(ArrayList<Snow> input){
Collections.sort(input);
}
}
//This is you public class Snow
//If you want to keep it in separate java file put it
public class Snow implements Comparable<Snow> {
private int vast;
private int prior;
private int ethnic;
private int remarkable;
public Snow(int vast, int prior, int ethnic, int remarkable) {
this.vast = vast;
this.prior = prior;
this.ethnic = ethnic;
this.remarkable = remarkable;
}
public int getEthnic() {
return ethnic;
}
public void setEthnic(int ethnic) {
this.ethnic = ethnic;
}
public int getPrior() {
return prior;
}
public void setPrior(int prior) {
this.prior = prior;
}
public int getVast() {
return vast;
}
public void setVast(int vast) {
this.vast = vast;
}
public int getRemarkable() {
return remarkable;
}
public void setRemarkable(int remarkable) {
this.remarkable = remarkable;
}
public int compareTo(Snow compareSnow) {
// TODO Auto-generated method stub
int compareThese = ((Snow) compareSnow).getRemarkable();
//ascending order
return this.remarkable - compareThese;
}
//This is added because when you use array list to print
//it will print remarkable of particular Snow object
#Override
public String toString() {
return String.valueOf(remarkable);
}
}
I have read quite a few pages of stackoverflow but I wasn't able to get my ArrayList to get copied unto another class. Here's the scenario, I'm building a quick book saver app, similar to what you would have in a library but simpler (for school).
I have my main library class (with the main) that has the swing set up for the main menu/options.
I have the book class with the constructor for new books as follows:
public class Livre {
private String titre;
private String soustitre;
private String auteur;
private String editeur;
private String collection;
private String isbn;
private long cup;
private double prixDeVenteSuggere;
private double prixVente;
private int nbPages;
private boolean disponible;
public Livre(String titre, String soustitre, String auteur, String editeur, String collection, String isbn, long cup, double prixDeVenteSuggere, double prixVente, int nbPages, boolean disponible){
this.titre = titre;
this.soustitre = soustitre;
this.auteur = auteur;
this.editeur = editeur;
this.collection = collection;
this.isbn = isbn;
this.cup = cup;
this.prixDeVenteSuggere = prixDeVenteSuggere;
this.prixVente = prixVente;
this.nbPages = nbPages;
disponible = true;
}
public Livre() {
}
public String getTitre() {
return titre;
}
public void setTitre(String titre) {
this.titre = titre;
}
public String getSoustitre() {
return soustitre;
}
public void setSoustitre(String soustitre) {
this.soustitre = soustitre;
}
public String getAuteur() {
return auteur;
}
public void setAuteur(String auteur) {
this.auteur = auteur;
}
public String getEditeur() {
return editeur;
}
public void setEditeur(String editeur) {
this.editeur = editeur;
}
public String getCollection() {
return collection;
}
public void setCollection(String collection) {
this.collection = collection;
}
public String getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
public long getCup() {
return cup;
}
public void setCup(long cup) {
this.cup = cup;
}
public double getPrixDeVenteSuggere() {
return prixDeVenteSuggere;
}
public void setPrixDeVenteSuggere(double prixDeVenteSuggere) {
this.prixDeVenteSuggere = prixDeVenteSuggere;
}
public double getPrixVente() {
return prixVente;
}
public void setPrixVente(double prixVente) {
this.prixVente = prixVente;
}
public int getNbPages() {
return nbPages;
}
public void setNbPages(int nbPages) {
this.nbPages = nbPages;
}
public boolean isDisponible() {
return disponible;
}
public void setDisponible(boolean disponible) {
this.disponible = disponible;
}
}
Option #1 on the Library class (built with WindowBuilder) has the "New" button which opens a second JFrame to input all the info in regards to the book.
in this JFrame class, I've added an actionListener on the confirm button to confirm the input on the JTextFields to be added as an object as follows:
public void confirmerLivre(){
l = new Livre(txtTitre.getText(), txtSousTitre.getText(), txtAuteur.getText(),
txtEditeur.getText(), txtCollection.getText(), txtISBN.getText(),
Long.parseLong(txtCodebar.getText()), Double.parseDouble(txtPrixMSRP.getText()),
Double.parseDouble(txtPrix.getText()), Integer.parseInt(txtPages.getText()), true);
confirmerLivre.add(l); /// confirmerLivre is defined as an ArrayList
}
What I can't wrap my head around is being able to take the ArrayList confirmerLivre from the 2nd JFrame class and push it unto my main JFrame class to be manipulated further with other options.
Any help will be greatly appreciated.
Thank you
Probably the quickest fix is to create/expose these methods in your main JFrame class:
getBookList()
setBookList()
When you create your popup JFrame, you need to pass an instance of your main JFrame class to it in its constructor:
public PopupFrame extends JFrame {
private MainFrame main;
public PopupFrame(MainFrame main) {
this.main = main;
}
}
Now that you have access to your main JFrame class from your popup, you can just go main.getBookList() to get the list (I'd recommend reading this question also)
If you create your ArrayList as a public variable in the second JFrame class (outside any of the methods) then it can be used in the first class such as:
SecondJFramesName.confirmerLivre()
In this code SecondJFramesName is the name of your second JFrame class. Now that your ArrayList is a public variable it can be accessed outside the class.
Note: your second JFrame's name is the one you use to create it in a way such as this:
JFrame SecondJFramesName = new JFrame("My title");
If you need any more specific details please comment!
Hopefully this helps!
Maybe observer pattern could help you:
public interface ConfirmerLivreMonitor{
void onConfirmerLivreChange(List<...> confirmerLivre);
}
then
//...
private ConfirmerLivreMonitor confirmerLivreMonitor;
public void setConfirmerLivreMonitor(ConfirmerLivreMonitor confirmerLivreMonitor ){
this.confirmerLivreMonitor = confirmerLivreMonitor
}
//....
public void confirmerLivre(){
l = new Livre(txtTitre.getText(), txtSousTitre.getText(), txtAuteur.getText(),
txtEditeur.getText(), txtCollection.getText(), txtISBN.getText(),
Long.parseLong(txtCodebar.getText()), Double.parseDouble(txtPrixMSRP.getText()),
Double.parseDouble(txtPrix.getText()), Integer.parseInt(txtPages.getText()), true);
confirmerLivre.add(l); /// confirmerLivre is defined as an ArrayList
if(confirmerLivreMonitor != null){ //notify confirmerLivre change
confirmerLivreMonitor.onConfirmerLivreChange(confirmerLivre);
}
}
make the Main JFrame implemnents ConfirmerLivreMonitor,so you can:
sencondJFrame.setConfirmerLivreMonitor(this);
or just pass a anonymous class:
sencondJFrame.setConfirmerLivreMonitor(new ConfirmerLivreMonitor(){
public void onConfirmerLivreChange(List<...> confirmerLivre){
//display in Main JFrame,maybe
}
});
once the confirmerLivre change, the main frame can display(or something else)
the first time,very cool
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I do not understand when my program reads my yml file, it read the type of float but it returns zero or 0.0. I have my float TRIG_EDGE_LEV which return 0.0 but it sould returns 1.5.
public class Loadyml {
//static OscilloDSO1072B dso1072B = new OscilloDSO1072B("visa://192.168.53.67/USB0::0x0957::0x0588::CN55040484::INSTR") ;
private String TRIG_EDGE_SLOP;
private String ENABLE_CHAN_OFFSET;
private float TRIG_EDGE_LEV;
private String TIM_MODE;
private String SCALE_NUMBER;
private String TIM_MAIN_SCAL;
private String SCALE_CHAN;
private String TRIG_EDGE_SOURCE;
private String WAV_SOUR;
private String PROB_CHAN;
private String INVERSE_CHAN;
private String WAV_POINT_MODE;
private String DISPLAY_CHAN;
private String ENABLE_CHAN_BWL_OFF;
private String COUPLING_CHAN;
private String OFFSET_NUMBER;
private String WAV_FORM;
public String getTRIG_EDGE_SLOP() {
return TRIG_EDGE_SLOP;
}
public void setTRIG_EDGE_SLOP(String TRIG_EDGE_SLOP) {
this.TRIG_EDGE_SLOP = TRIG_EDGE_SLOP;
}
public String getENABLE_CHAN_OFFSET() {
return ENABLE_CHAN_OFFSET;
}
public void setENABLE_CHAN_OFFSET(String ENABLE_CHAN_OFFSET) {
this.ENABLE_CHAN_OFFSET = ENABLE_CHAN_OFFSET;
}
public float getTRIG_EDGE_LEV() {
return TRIG_EDGE_LEV;
}
public void setTRIG_EDGE_LEV(float TRIG_EDGE_LEV) {
this.TRIG_EDGE_LEV = TRIG_EDGE_LEV;
}
public String getTIM_MODE() {
return TIM_MODE;
}
public void setTIM_MODE(String TIM_MODE) {
this.TIM_MODE = TIM_MODE;
}
public String getSCALE_NUMBER() {
return SCALE_NUMBER;
}
public void setSCALE_NUMBER(String SCALE_NUMBER) {
this.SCALE_NUMBER = SCALE_NUMBER;
}
public String getTIM_MAIN_SCAL() {
return TIM_MAIN_SCAL;
}
public void setTIM_MAIN_SCAL(String TIM_MAIN_SCAL) {
this.TIM_MAIN_SCAL = TIM_MAIN_SCAL;
}
public String getSCALE_CHAN() {
return SCALE_CHAN;
}
public void setSCALE_CHAN(String SCALE_CHAN) {
this.SCALE_CHAN = SCALE_CHAN;
}
public String getTRIG_EDGE_SOURCE() {
return TRIG_EDGE_SOURCE;
}
public void setTRIG_EDGE_SOURCE(String TRIG_EDGE_SOURCE) {
this.TRIG_EDGE_SOURCE = TRIG_EDGE_SOURCE;
}
public String getWAV_SOUR() {
return WAV_SOUR;
}
public void setWAV_SOUR(String WAV_SOUR) {
this.WAV_SOUR = WAV_SOUR;
}
public String getPROB_CHAN() {
return PROB_CHAN;
}
public void setPROB_CHAN(String PROB_CHAN) {
this.PROB_CHAN = PROB_CHAN;
}
public String getINVERSE_CHAN() {
return INVERSE_CHAN;
}
public void setINVERSE_CHAN(String INVERSE_CHAN) {
this.INVERSE_CHAN = INVERSE_CHAN;
}
public String getWAV_POINT_MODE() {
return WAV_POINT_MODE;
}
public void setWAV_POINT_MODE(String WAV_POINT_MODE) {
this.WAV_POINT_MODE = WAV_POINT_MODE;
}
public String getDISPLAY_CHAN() {
return DISPLAY_CHAN;
}
public void setDISPLAY_CHAN(String DISPLAY_CHAN) {
this.DISPLAY_CHAN = DISPLAY_CHAN;
}
public String getENABLE_CHAN_BWL_OFF() {
return ENABLE_CHAN_BWL_OFF;
}
public void setENABLE_CHAN_BWL_OFF(String ENABLE_CHAN_BWL_OFF) {
this.ENABLE_CHAN_BWL_OFF = ENABLE_CHAN_BWL_OFF;
}
public String getCOUPLING_CHAN() {
return COUPLING_CHAN;
}
public void setCOUPLING_CHAN(String COUPLING_CHAN) {
this.COUPLING_CHAN = COUPLING_CHAN;
}
public String getOFFSET_NUMBER() {
return OFFSET_NUMBER;
}
public void setOFFSET_NUMBER(String OFFSET_NUMBER) {
this.OFFSET_NUMBER = OFFSET_NUMBER;
}
public String getWAV_FORM() {
return WAV_FORM;
}
public void setWAV_FORM(String WAV_FORM) {
this.WAV_FORM = WAV_FORM;
}
public void Loadfichier() throws FileNotFoundException{
try {
System.out.println(Yaml.loadType(new File("config.yml"), Loadyml.class));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public String toString() {
//dso1072B.setchanconftest(commande);
// Float.parseFloat(SCALE_NUMBER);
return "[Commande SCPI='"+WAV_FORM+","+ENABLE_CHAN_BWL_OFF+""
+ ","+TRIG_EDGE_LEV+","+TIM_MODE+""
+ ","+SCALE_NUMBER+","+TRIG_EDGE_SLOP+""
+ ","+DISPLAY_CHAN+","+TIM_MAIN_SCAL+""
+ ","+SCALE_CHAN+","+TRIG_EDGE_SOURCE+""
+ ","+WAV_SOUR+","+INVERSE_CHAN+""
+ ","+PROB_CHAN+","+WAV_POINT_MODE+""
+ ","+ENABLE_CHAN_OFFSET+","+COUPLING_CHAN+""
+ ","+OFFSET_NUMBER+"']";
}
}
How can i fix that ?
Console screen
To solve your issue - use double instead of float - Somewhere inside Jyaml's codebase it tries to match floating point numbers to getter/setter methods (not fields) of type double reflectively. As your field is of type float, reflection based matching fails and you get the default value of 0.0f
On a side note :
Use Boxed/object versions of fields and methods, i.e. - Double instead of double. null make more sense than 0.0 for existence check. (I'd also recommend java 8's Optional).
Jyaml is super old and its codebase looks horrendous , please consider using a newer alternative for yaml parsing, preferably one that uses annotations like Jackson.
With Jackson - use annotations to map names in yaml file as they are and keep your code according to java conventions ( fields and methods should be camel cased)