Boolean wrong output - java

my code:
public class Kuh {
private String name;
private boolean istSatt;
public Kuh(String name, boolean istSatt) {
}
public double gibMilch() {
if (istSatt == true) {
System.out.println(10.0);
return 10.0;
} else {
System.out.println(3.0);
return 3.0;
}
}
public void grasen() {
istSatt = true;
}
public static void main(String[] args) {
Kuh Frida = new Kuh("Frida", true);
Frida.gibMilch();
Frida.grasen();
Frida.gibMilch();
}
}
My problem: I set "istSatt" of the object "Frida" to "true" at creation. So when using the method "gibMilch", it should put out "10". Despite that, it puts out "3", like the boolean would be false, even tho I set it to true. It only puts out "10" after using "grasen".
What did I do wrong?

You are not assigning the constructor parameters to the fields.
public Kuh(String name, boolean istSatt) {
this.name = name;
this.istSatt = istSatt;
}

You need to set your class variable values in your constructor:
public Kuh(String name, boolean istSatt) {
this.name = name;
this.istSatt = istSatt;
}
Here this refers to the class you are instantiating.

Try this instead, as you did not seem to assign to anything on the call to Kuh Frida = new Kuh("Frida", true);
i.e.
public class Kuh {
private String name;
private boolean istSatt;
public Kuh(String name, boolean istSatt) {
this.istSatt=istSatt;
this.name=name;
}
public double gibMilch() {
if (istSatt) {
System.out.println(10.0);
return 10.0;
} else {
System.out.println(3.0);
return 3.0;
}
}
public void grasen() {
istSatt = true;
}
public static void main(String[] args) {
Kuh Frida = new Kuh("Frida", true);
Frida.gibMilch();
Frida.grasen();
Frida.gibMilch();
}
}

Related

Created a method that returns a true value is the object is 5+ years old

I want to create a method which returns a Boolean value. It should return true if the sale is over 5 years old or older and false if the sale is under 5 years.
I have given it an attempt but I cannot seem to get it work. If anyone can think over a more efficient way of completing this I am open to a different direction.
I have tried the below but the method continue to error out no matter what I change. Google has not been helpful either as the method is so specific.
the method is called isSaleOld() (Go to the bottom of the code)
Thank you for any help in advance. Code is below for the whole project.
public class Sale
{
// instance variables
private String company;
private String yearBought;
private int saleValue;
private Seller seller;
public Sale(String aCompany, String aYear, int aValue,
Seller theSeller)
{
this.company = aCompany;
this.yearBought = aYear;
this.saleValue = aValue;
this.seller = theSeller;
}
public void setSellersName(String aName)
{
this.seller.setName(aName);
}
public void setSaleValue(int aValue)
{
this.saleValue = aValue;
}
public void setyearBought(String aYear)
{
this.yearBought = aYear;
}
public void setCompany(String aCompany)
{
this.company = aCompany;
}
public int getSaleValue()
{
return this.saleValue;
}
public boolean isApprovalRequired()
{
return this.getSaleValue() >=10000;
}
public String getYearBought()
{
return this.yearBought;
}
public void isSaleOld(String[] args)
{
yearBought = getYearBought("2020");
boolean after = yearBought.after(yearBought);
}
}
import java.time.LocalDateTime;
public class Sale {
private int yearBought;
public void sale(int year) {
setYearBought(year);
}
private int getYearBought() {
return this.yearBought;
}
private void setYearBought(int year) {
this.yearBought = year;
}
public boolean isSaleOld() {
int now = LocalDateTime.now().getYear();
int yearBought = getYearBought();
if (now - yearBought > 5) {
System.out.println("Yes, older than 5 years");
return true;
}
System.out.println("No, less than 5 years old");
return false;
}
public static void main(String[] args) {
Sale sale = new Sale();
Sale sale2 = new Sale();
sale.sale(2010);
sale2.sale(2020);
sale.isSaleOld();
sale2.isSaleOld();
}

Java programming error Pass by reference

I have an java application where a object reference "Validate.Options" is passed as parameter to the function "ValidateResult(Validate.Options option)" and the function is called iterative. Within this function based on the certain condition the property "enableProcessing" of the passed object gets changed which does not get reset on the next iterate. How can I reset this property?
Below is the sample code.
public interface Validate
{
public List validate();
public class Options implements Serializable
{
public String name;
public boolean enableProcessing = true;
public Options(String name)
{
this.name = name;
}
}
}
public class Coder
{
public String name;
public int age;
public Coder(String name, int age)
{
this.name = name;
this.age = age;
}
public void ValidateResult(Validate.Options option)
{
if(option.name.equals(this.name) && option.enableProcessing)
{
option.enableProcessing = false;
//
//business logic and function call
//
}
}
public static void main(String[] args)
{
Validate.Options options = new Validate.Options("Test");
List<Coder> coders = new ArrayList<Coder>();
Coder coder = new Coder("Test", 28);
Coder coder1 = new Coder("XYZ", 18);
Coder coder2 = new Coder("Test", 16);
coders.add(coder);
coders.add(coder1);
coders.add(coder2);
for(Coder co : coders)
{
co.ValidateResult(options);
}
}
}
If I understood the question well - in your for loop, simply add a line of code to reset the value of your public Validate.Options.enableProcessing field
for(Coder co : coders)
{
//reset options object for the next iteration
options.enableProcessing = true;
co.ValidateResult(options);
}
Make options immutable if you do not want it to be changed:
public class Options implements Serializable
{
public final String name; // final prevents changes
public final boolean enableProcessing = true; // final prevents changes
public Options(String name)
{
this.name = name;
}
}
To locally work with enableProcessing copy its value to a local variable.
public void ValidateResult(Validate.Options option)
{
boolean enableProcessing = option.enableProcessing; // create local copy
if(option.name.equals(this.name) && enableProcessing) // use local copy
{
enableProcessing = false; // only change local copy
//
//business logic and function call
//
}
}
Alternatively create new, fresh Options for each loop:
public static void main(String[] args)
{
List<Coder> coders = Arrays. asList(
new Coder("Test", 28),
new Coder("XYZ", 18),
new Coder("Test", 16)
);
for(Coder co : coders)
{
Validate.Options options = new Validate.Options("Test"); // fresh options for each iteration
co.ValidateResult(options);
}
}

Feeding a list of signals into wires

I'm given private List<Wire> inputs and a method public void feed(List<Signal> inSigs). I have to change signals (initialy each signal is ==Signal.X) in the List<Wire> inputs with the inSigs given in the parameter of the method feed(). THat's all I've been having trouble with. How could I change the state of List inputs with passed inSigs (notice: the parameter is of type <Signal>)? I've done smth but constantly getting and underline error under setSignal(x). I'm attached two classes (Gate and Wire below)
import java.util.*;
public abstract class Gate implements Logic {
private List<Wire> inputs;
private Wire output;
private String name;
public Gate(String name, List<Wire> ins, Wire out)
{
this.name = name;
this.output = out;
if(ins.size() == 0 || ins.isEmpty())
throw new ExceptionLogicParameters(false, 1, 0);
else
this.inputs = ins;
}
#Override
public void feed(List<Signal> inSigs)
{
for(Signal x: inSigs)
inputs.setSignal(x);
}
#Override
public void feed(String name)
{
((Wire) inputs).setName(name);
}
}
public class Wire {
private Signal signal;
private String name;
public Wire(String name)
{
this.name = name;
this.signal = Signal.X;
}
#Override
public String toString()
{
return "\""+ this.name+":"+this.signal+"\"";
}
#Override
public boolean equals(Object other)
{
if(other instanceof Wire)
{
Wire leftHandside = (Wire)other;
return this.name.equals(leftHandside.name) && this.signal == leftHandside.signal;
}
else
return false;
}
public Signal getSignal()
{
return this.signal;
}
public String getString()
{
return this.name;
}
public void setSignal(Signal signal)
{
this.signal = signal;
}
public void setName(String name)
{
this.name = name;
}
}
There is a bunch of ambiguity in the way your code and question reads.
I'll assume that the list of signals is the same size as your private list of wires, then:
public void feed(List<Signal> inSigs) {
// Needs precondition that inSigs.size() == input.size()
for (int i = 0; i < inSigs.size(); i++) {
inputs.get(i).setSignal(inSigs.get(i));
}
}
Otherwise you need a way to map your signals to wires, (probably by index).
Probably you need something like this then:
#Override
public void feed(List<Signal> inSigs)
{
if(inSigs.size() != inputs.size()) {
throw new ExceptionLogicParameters(false, 1, 0);
}
int i = 0;
for (Signal x: inSigs) {
inputs.get(i++).setSignal(x);
}
}

(identifier expected) getter/setter and objects

I've got a problem with my programm. When i try to compile following i just receive the message:
Tutorium.java:15: error: <identifier> expected
public void settName(vorlesung.lectureName) {
^
So my Code:
Tutorium.java
public class Tutorium {
private Vorlesung vorlesung;
public String tName;
private int tNumber;
public int gettNumber() {
return this.tNumber;
}
public String gettName() {
return this.tName;
}
public void settName(vorlesung.lectureName) {
this.tName = vorlesung.lectureName;
}
public String toString() {
return (this.tName + ", " + this.tNumber);
}
public Tutorium(int tNumber){
this.tNumber = tNumber; } }
Vorlesung.java
public class Vorlesung {
public String lectureName;
private int lectureNumber;
private int lecture;
private Dozent dozent;
private String lecturerlName;
public String getlectureName(){
return this.lectureName;
}
public int lectureNumber(){
return this.lectureNumber;
}
public int lecture(){
return this.lecture;
}
public String getlecturer(){
this.lecturerlName = dozent.lecturerlName;
return this.lecturerlName;
}
public String toString() {
return (this.lectureName + ", " + this.lectureNumber);
}
public Vorlesung(String lectureName, int lecture) {
this.lectureName = lectureName;
this.lecture = lecture +1;
this.lectureNumber = this.lecture -1;
this.lecturerlName = lecturerlName;
}}
My Main-Method:
public class MainVorlesung {
public static void main(String[] args) {
Student student = new Student("STUDENTNAME", "STUDENTLASTNAME", 178, 1);
Vorlesung vorlesung = new Vorlesung("Programmieren", 13341);
Tutorium tutorium = new Tutorium(3);
Dozent dozent = new Dozent("LECTURERFIRSTNAME", "LECTURERLASTNAME", 815);
System.out.println(student.toString());
System.out.println(vorlesung.toString());
System.out.println(tutorium.toString());
System.out.println(dozent.toString());
}}
My goal is to set the value of tName equal the value of vorlesung.lectureName.
Why can't i do this that way?
I appreciate every help. :)
Thanks
For methods, the arguments that you pass in must have a declared value.
In this case, a String. So you need to change your method to this:
public void settName(String newLectureName) {
this.tName = newLectureName;
}
Read more about what a java method is and how to create one here: http://www.tutorialspoint.com/java/java_methods.htm
Change settName to
public void settName(String name) {
this.tName = name;
}
Since your goal is:
My goal is to set the value of tName equal the value of vorlesung.lectureName.
You should get rid of the setName method entirely since it will depend entirely on the vorlesung field and so should not be changeable. You should also get rid of the tName field, and instead change getName() to:
public class Tutorium {
private Vorlesung vorlesung;
// public String tName; // get rid of
private int tNumber;
public String gettName() {
if (vorlesung != null) {
return vorlesung.getlecturer();
}
return null; // or throw exception
}
// *** get rid of this since you won't be setting names
// public void settName(Vorlesung vorlesung) {
// this.tName = vorlesung.lectureName;
// }
I have just now noticed that your Tutorium class does not have and absolutely needs a setVorlesung(...) method.
public void setVorlesung(Vorlesung vorlesung) {
this.vorlesung = vorlesung;
}

BeanUtils.setProperty not working for Boolean values?

I have this simple Bean class and try to set some values with BeanUtils.setProperty Problem is, it seems that String works just fine, but when I try to set a Boolean value it just does not work. I have tried and define the field as public but still not working. Any help? Why is this not working?
public class TestBean {
protected Boolean someBoolean;
protected String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isSomeBoolean() {
if (someBoolean == null) {
return true;
} else {
return someBoolean;
}
}
public void setSomeBoolean(Boolean value) {
this.someBoolean = value;
}
public static void main(String[] args) {
TestBean o = new TestBean();
Boolean b = new Boolean(false);
BeanUtils.setProperty(o, "someBoolean", b);
BeanUtils.setProperty(o, "name", "A name");
System.out.println(((TestBean)o).isSomeBoolean());
// Output = true WHY?????
System.out.println(((TestBean)o).getName());
// Output = A name
BeanUtils.setProperty(o, "someBoolean", false);
BeanUtils.setProperty(o, "name", "Another name");
System.out.println(((TestBean)o).isSomeBoolean());
// Output = true WHY????
System.out.println(((TestBean)o).getName());
// Output = Another name
}
}
You need to change it from
protected Boolean someBoolean;
to
protected boolean someBoolean;
You will get more info from here.
Java Beans, BeanUtils, and the Boolean wrapper class

Categories

Resources