Feeding a list of signals into wires - java

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);
}
}

Related

Boolean wrong output

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();
}
}

How to sort an array that is combined by "null" and Strings?

I have got an array of 20:
private Karte[] deckArr;
deckArr = new Karte[20];
Now I want to sort the array by card-names every time a new card is added.
P.S. the cards are added 1 by 1 after clicking on a button, so there are empty spaces in the array.
Since the...
Arrays.sort(deckArr.getName());
...method does not work here I asked myself how it is done.
Karte(card) class:
package Model;
/**
* Created by 204g07 on 18.03.2016.
*/
public class Karte implements ComparableContent<Karte>{
private int schoenheit;
private int staerke;
private int geschwindigkeit;
private int intelligenz;
private int coolness;
private int alter;
private String seltenheit;
private String name;
public Karte(String pName, int pSchoenheit,int pStaerke,int pGeschwindigkeit, int pIntelligenz, int pCoolness, int pAlter, String pSeltenheit ) {
name=pName;
schoenheit=pSchoenheit;
staerke=pStaerke;
geschwindigkeit=pGeschwindigkeit;
intelligenz=pIntelligenz;
coolness=pCoolness;
alter=pAlter;
seltenheit=pSeltenheit;
}
//getter
public int getSchoenheit(){
return schoenheit;
}
public int getStaerke(){
return staerke;
}
public int getGeschwindigkeit(){
return geschwindigkeit;
}
public int getIntelligenz(){
return intelligenz;
}
public int getCoolness(){
return coolness;
}
public int getAlter(){
return alter;
}
public String getSeltenheit(){
return seltenheit;
}
public String getName(){
return name;
}
//setter
public void setSchoenheit(int pSchoenheit){
schoenheit = pSchoenheit;
}
public void setStaerke(int pStaerke){
staerke = pStaerke;
}
public void setGeschwindigkeit(int pGeschwindigkeit){
geschwindigkeit = pGeschwindigkeit;
}
public void setIntelligenz(int pIntelligenz){
intelligenz = pIntelligenz;
}
public void setCoolness(int pCoolness){
coolness = pCoolness;
}
public void setAlter(int pAlter){
alter = pAlter;
}
public void setSeltenheit(String pSeltenheit){
seltenheit = pSeltenheit;
}
public void setName(String pName){
name = pName;
}
#Override
public boolean isLess(Karte karte) {
if (getName().compareTo(karte.getName()) < 0){
return true;
}else{
return false;
}
}
#Override
public boolean isEqual(Karte karte) {
return getName() == karte.getName();
}
#Override
public boolean isGreater(Karte karte) {
if (getName().compareTo(karte.getName()) > 0){
return true;
}else{
return false;
}
}
}
Any help is appreciated!
Why not just use ArrayList instead? Easier to add, remove elements and you will never have empty slots.
Anyway to sort you can use Collections.sort like this:
deckArr = new ArrayList<Karte>();
Collections.sort(deckArr, Comparator.comparing(karte -> karte.getName()));
Java 8 offers a simple solution:
The Comparable Interface has a static method that creates a Comaprator with an extractor.
Comparator<Card> comp = Comparator.comparing(Karte::getName);
With this using a sorting method (e.g. Arrays.sort) is easy to call.
On top of that, to solve your nullpointer problem, the Comparator Interface offers another two functions: NullsLast and nullsFirst.
Comparator<Card> comp = Comparator.nullsLast(Comparator.comparing(Card::getName));
For me this looks like the easiest solution to your question :)
This should solve your problem. Implements the Comparable interface.
/**
* Created by 204g07 on 18.03.2016.
*/
public class Karte implements Comparable<Karte>{
private int schoenheit;
private int staerke;
private int geschwindigkeit;
private int intelligenz;
private int coolness;
private int alter;
private String seltenheit;
private String name;
public Karte(String pName, int pSchoenheit,int pStaerke,int pGeschwindigkeit, int pIntelligenz, int pCoolness, int pAlter, String pSeltenheit ) {
name=pName;
schoenheit=pSchoenheit;
staerke=pStaerke;
geschwindigkeit=pGeschwindigkeit;
intelligenz=pIntelligenz;
coolness=pCoolness;
alter=pAlter;
seltenheit=pSeltenheit;
}
//getter
public int getSchoenheit(){
return schoenheit;
}
public int getStaerke(){
return staerke;
}
public int getGeschwindigkeit(){
return geschwindigkeit;
}
public int getIntelligenz(){
return intelligenz;
}
public int getCoolness(){
return coolness;
}
public int getAlter(){
return alter;
}
public String getSeltenheit(){
return seltenheit;
}
public String getName(){
return name;
}
//setter
public void setSchoenheit(int pSchoenheit){
schoenheit = pSchoenheit;
}
public void setStaerke(int pStaerke){
staerke = pStaerke;
}
public void setGeschwindigkeit(int pGeschwindigkeit){
geschwindigkeit = pGeschwindigkeit;
}
public void setIntelligenz(int pIntelligenz){
intelligenz = pIntelligenz;
}
public void setCoolness(int pCoolness){
coolness = pCoolness;
}
public void setAlter(int pAlter){
alter = pAlter;
}
public void setSeltenheit(String pSeltenheit){
seltenheit = pSeltenheit;
}
public void setName(String pName){
name = pName;
}
public int compareTo(Karte karte) {
return this.name.compareTo(karte.getName());
}
}
Then you just need to call Arrays.sort(deckArr);
You need to check for nulls and just call below--
Arrays.sort(deckArr, new Comparator<Karte>() {
#Override
public int compare(Karte karte1, Karte karte2) {
if (karte1.getName() == null && karte2.getName() == null) {
return 0;
}
if (karte1.getName() == null) {
return 1;
}
if (karte2.getName() == null) {
return -1;
}
return karte1.getName().compareTo(karte2.getName());
}});

A combination of methods in a list

In a Gate class I have method public List<Signal> inspect(List<Signal> inputs) which should contain a combination of feed(), propagate(), and read(). That's the only method I have left to finish but getting an error. Could smb please help me with this method? NOTE: propagate() is left abstract to be overriden by childclasses of Gate class. The method public List<Signal> inspect(List<Signal> inputs) should combine feed(), propagate(), and read().
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)
{
if(inSigs.size() != inputs.size())
throw new ExceptionLogicParameters(false, inputs.size(), inSigs.size());
else
{
for(int i = 0; i < inSigs.size(); i++)
{
inputs.get(i).setSignal(inSigs.get(i));
}
}
}
#Override
public void feed(String name)
{
if(!(this.name.equals(name)))
throw new ExceptionLogicMalformedSignal(name.charAt(0), "Invalid logic input");
else
{
Signal signalValue = Signal.fromString(name.charAt(0));
}
}
#Override
public List<Signal> read()
{
List<Signal> signals = new ArrayList<>();
signals.add(output.getSignal());
return signals;
}
#Override
public abstract boolean propagate();
#Override
public List<Signal> inspect(List<Signal> inputs)
{
List<Signal> allMethods = new ArrayList<>();
allMethods.add(this.feed(inputs));
allMethods.add(this.propagate());
allMethods.add(this.read());
}
#Override
public String toString()
{
return this.name+"( " + inputs.toString() + " | " + output.toString() + " )";
}
#Override
public boolean equals(Object other)
{
if(other instanceof Gate)
{
Gate someGate = (Gate)other;
return (this.inputs == someGate.inputs) && (this.output.equals(someGate.output)
&& (this.name.equals(someGate.name)));
}
else
return false;
}
}
All your methods have no return type.
When you do this
allMethods.add(this.feed(inputs));
allMethods.add(this.propagate());
allMethods.add(this.read());
It would not return anything and hence nothing is added to the list which will throw error.
Your list of of type signal
List<Signal> allMethods = new ArrayList<>();
You need to change the return type of all methods to Signal to add them to the list. Like you cant add an Integer to a List<String> you cannot add anything else than type Signal to the List<Signal>
I'm no sure of your code logic and if you can change the return type or not, but changing all methods return type to Signal should work fine.
Also, you need a return statement for
public List<Signal> inspect(List<Signal> inputs)
You have to always return something if method is not void and the return type should be same as function type

What is the correct way to do this?

I know this must be a fundamental design problem because I clearly can't do this. I want to call the ownGrokk, ownTyce, etc methods from another class depending on the value of the integer assigned to OwnedSpirits(int). This in turn fills arrays.
The problem is, I do this multiple times, and doing it from another class it seems like I have to make a new object every time to pass the new int argument, and doing so resets the value of spiritInstance. And, since that resets to zero, the arrays don't fill properly. I try to print out my array values later and I get an "ArrayIndexOutOfBoundsException".
public class OwnedSpirits {
private int spiritTypeInt = 0;
public static int spiritInstance=0;
public static int[] spiritarray = new int[9];
public static String[] spiritName = new String[9];
public static int[] party = new int[3];
public OwnedSpirits(int spiritcall){
if(spiritcall == 1){
ownGrokk();
}
if(spiritcall == 2){
ownRisp();
}
if(spiritcall == 3){
ownTyce();
}
if(spiritcall == 4){
ownDaem();
}
if(spiritcall == 5){
ownCeleste();
}
}
private void ownGrokk(){
spiritName[spiritInstance] = "Grokk";
spiritInstance++;
}
private void ownRisp(){
spiritName[spiritInstance] = "Risp";
spiritInstance++;
}
private void ownDaem(){
spiritName[spiritInstance] = "Daem";
spiritInstance++;
}
private void ownCeleste(){
spiritName[spiritInstance] = "Celeste";
spiritInstance++;
}
private void ownTyce(){
spiritName[spiritInstance] = "Tyce";
spiritInstance++;
}
and this code is in another class, where it attempts to call the methods to fill the array
buttonConfirm.addListener(new ClickListener(){
#Override
public void clicked(InputEvent event, float x, float y) {
if(xcounter==3){
for(x=0; x<3; x++){
if(setdaemtrue == true){
new OwnedSpirits(4);
}
if(setrisptrue == true){
new OwnedSpirits(2);
}
if(setcelestetrue == true){
new OwnedSpirits(5);
}
if(settycetrue == true){
new OwnedSpirits(3);
}
if(setgrokktrue == true){
new OwnedSpirits(1);
}
}
}
}
});
and finally in yet another class:
System.arraycopy(OwnedSpirits.spiritName, 0, partylist, 0, 3);
#Override
public void show() {
System.out.println(partylist[0]);
System.out.println(partylist[1]);
System.out.println(partylist[2]);
spiritlist.setItems(partylist);
table.add(spiritlist);
table.setFillParent(true);
stage.addActor(table);
}
If the last part is confusing, it's because I am using libgdx. the print statements are there just to try to figure out why my list was having an error
I can show you what I would do to handle Spirits, and Parties.
The Spirit class, contains name and current party its assigned to:
package com.stackoverflow.spirit;
public class Spirit {
private String name;
private Party party;
private SpiritType type;
private static int id = 0;
public static enum SpiritType {
Grokk, Risp, Tyce, Daem, Celeste
};
public Spirit(String name, SpiritType type) {
create(name, type);
}
public Spirit(SpiritType type) {
create(null, type);
}
// This is to handle Java inexistance of default parameter values.
private void create(String name, SpiritType type)
{
Spirit.id++;
this.name = (name == null) ? (type.name() + " " + id) : name;
this.type = type;
}
public String getName() {
return name;
}
public Party getParty() {
return party;
}
public SpiritType getType() {
return type;
}
/**
* Used internally by #see Party
* #param party the party this Spirit belongs
*/
public void setParty(Party party) {
this.party = party;
}
public void setName(String name) {
this.name = name;
}
#Override
public String toString()
{
return this.name;
}
}
Finally the Party class, contains a set of Spirits, you can add and remove Spirits from the party.
package com.stackoverflow.spirit;
import java.util.HashSet;
public class Party {
private HashSet<Spirit> spirits = new HashSet<Spirit>();
private static int id = 0;
private String name = "Party " + Party.id++;;
public Party() {
}
public Party(String name) {
this.name = name;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void add(Spirit spirit) {
if (!spirits.contains(spirit)) {
spirits.add(spirit);
if (spirit.getParty() != null) {
//Remove from previous party to update the other party set
spirit.getParty().remove(spirit);
}
spirit.setParty(this);
} else {
// throw new SpiritAlreadyOnParty();
}
}
public void remove(Spirit spirit)
{
if (spirits.contains(spirit))
{
spirit.setParty(null); // You could create a default empty party for "Nature/Neutral" Spirits perhaps :)
spirits.remove(spirit);
}
else {
//throw new SpiritNotInParty();
}
}
public boolean isOnParty(Spirit spirit) {
return spirits.contains(spirit);
}
public ArrayList<Spirit> getSpirits()
{
return new ArrayList<Spirit>(spirits);
}
public int getPartySize() {
return spirits.size();
}
public String getPartyInfo()
{
StringBuilder builder = new StringBuilder();
builder.append("Party:" + this.name + " Size:" + this.spirits.size() + "\n");
for (Spirit s : spirits)
{
builder.append(s.getName() + "\n");
}
return builder.toString();
}
#Override
public String toString()
{
return this.name;
}
}
Here I use the Spirit and Party classes, you could add more functionality, like properties for party strength, magic buffs on the party, etc:
package com.stackoverflow.spirit;
import com.stackoverflow.spirit.Spirit.SpiritType;
public class Main {
public static void main(String[] args) throws java.lang.Exception {
Party griffindor = new Party("Griffindor"), slytherin = new Party(
"Slytherin");
// You can also do for (SpiritType type : SpiritType.values() then
// type.ordinal()
for (int i = 0; i < SpiritType.values().length; i++) {
griffindor.add(new Spirit(SpiritType.values()[i]));
slytherin.add(new Spirit(SpiritType.values()[i]));
}
Spirit mySpirit = new Spirit("NotAHPFan", SpiritType.Celeste);
slytherin.add(mySpirit);
System.out.println("Name of party:" + mySpirit.getParty().getName());
System.out.println("Is on griffindor?:"
+ griffindor.isOnParty(mySpirit));
// What now?
griffindor.add(mySpirit);
System.out.println("Is " + mySpirit.getName() + " on "
+ slytherin.getName() + "?:" + slytherin.isOnParty(mySpirit));
System.out.println(mySpirit.getName() + " is now on "
+ mySpirit.getParty() + "\n");
System.out.println(griffindor.getPartyInfo());
System.out.println(slytherin.getPartyInfo());
}
}
P.D: I'm not a HP fan.

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