I want to check if a boolean variable from a Domain class equates to true in my method.
private Boolean exampleName1 = null;
This is in a #Entity class called Javaclass1.java
My method is in JavaClass2.java
public static String myMethod(name1, nameClass){
if(nameClass != null){
return name1;
}
}
I want to execute the if condition above only if the boolean for exampleName1 in the entity class is true. It has a getter and setter.
How would I import and setup the if condition?
If you want to read a private attribute from another entity (in your example something like getExampleName1()) you should have instance of that entity at first. you should import your entity by Name and then call the getter name. for example:
public static String myMethod(String name1, String nameClass, Javaclass1 javaClass1){
if(nameClass != null && javaClass1.getExampleName1 == true ){
return name1;
}
}
Related
I have a HashMap<CustomClass1, ArrayList<CustomClass2>> called map.
Is it possible to get the ArrayList<CustomClass2> using a String contained within CustomClass1? e.g. I would like to say map.get("zone1") and get the matching ArrayList. This is for a school project and I have to use the HashMap described above. This resembles my code:
public class Main {
public static void main(String [] args) {
HashMap<CustomClass1, ArrayList<CustomClass2>> map = new HashMap<CustomClass1, ArrayList<CustomClass2>();
CustomClass1 example = new CustomClass1("zone1");
map.put(example, new ArrayList<CustomClass2>());
//Later in the code where I don't have the objects in scope (except for map)...
ArrayList<CustomClass2> value = map.get("zone1");
}
class CustomClass1 {
private final String name;
//Additional variables
public CustomClass1 (String name) {
this.name = name;
}
#Override
public int hashCode() {
return name.hashCode();
}
#Override
public boolean equals(Object obj) {
...
String test = (String) obj;
if(test.equals(this.name))
return true;
...
}
}
}
So far I have tried overriding the hashCode() and equals() methods in my CustomClass1 so that it uses the String's hashCode. From my understanding, when I call the map.get(obj) method, it calls obj.hashCode() to find the correct bucket and then obj.equals() to see if it is the correct object. I think I am wrong in thinking this, since I keep getting null values as a result and the equals() method in CustomClass1 is never called.
See comments for workaround/solution.
assertArrayEquals is not working for user defined class.
I have User class which has name property alone. When I create an array objects (actual & expected) for User class and try to test with assertArrayEquals, the test fails.
I am getting below error
arrays first differed at element [0]; expected:<cts.Learning.User#3fa77460> but was:<cts.Learning.User#619a5dff>"
I am using Junit - 4.12 and Hamcrest-all 1.3 version.
public class User {
private String name;
public User(){}
public User(String name) {
this.name = name;
}
public String getName(){
return name;
}
public void setName(String name) {
this.name = name;
}
}
import org.junit.*;
import static org.junit.Assert.*;
public class UserJunit {
#Test
public void testUserBO() {
User[] expected = new User[2];
expected[0] = new User("A");
expected[1] = new User("B");
User[] actual = new User[2];
actual[0] = new User("A");
actual[1] = new User("B");
assertArrayEquals(actual, expected);
}
}
The assertArrayEquals method - after handling iteration and various other features - eventually resolves to this call:
expected.equals(actual)
So, it invokes equals on your User class and since you have not declared an equals method this delegates to the the default implementation of equals in the Object class which compares using the equality operator i.e. it compares the object references not their value.
So, the assertArrayEquals call ends up invoking:
actual[0] == expected[0]
And since they are not the same instance that assertion fails.
If you want to assert equality between these instances of User then you'll have to provide some definition of what it means for two instances of User to be equal i.e. define an equals method on User.
Here's an example:
#Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
User user = (User) o;
return Objects.equals(name, user.name);
}
#Override
public int hashCode() {
return Objects.hash(name);
}
If you add the above to your User class then your test will pass.
This has been bugging me for a while. I am new to serious OOps concepts and came across. Here's what I have:
Class ape:
public class ape {
String name;
public String getBehavior() {
return "I am apeshit";
}
}
Class humano:
public class humano extends ape {
public String name;
public humano(String namevalue) {
name = namevalue;
}
public String getBehavior() {
return "I am a :" + name;
}
public String getName() {
return name;
}
}
Class neandarthal:
public class neandarthal extends ape {
String name;
public neandarthal(String name) {
super.name = name;
}
public String getBehavior() {
return "I am a :" + name;
}
public String getName() {
return name;
}
}
Species_Factory Class:
public class species_factory {
static ape ap = null;
static ape getSpecies(String name) {
if (name == "humano") {
return new humano(name);
}
if (name == "neandarthal") {
return new neandarthal(name);
}
else {
return ap;
}
}
}
And Finally the main class species:
public class species {
public static void main(String args[]) {
ape a1 = species_factory.getSpecies("humano");
ape a2 = species_factory.getSpecies("neandarthal");
System.out.println(a1.name);
System.out.println(a2.name);
System.out.println(a1.getBehavior());
System.out.println(a2.getBehavior());
System.out.println(a1.getClass().getSuperclass().getName());
}
}
The output:
null
neandarthal
I am a :humano
I am a :null
ape
The query I have is regarding the null value. Basically when I try to display System.out.println(a1.name). I am seeing null. Why is this?
I see that I am getting the value I am a :humano in the third line, which means that the value I pass for the string variable name is getting stored in the humano instance.
But why is it not displayed when I try to display the same variable using the statement System.out.println(a1.name)?
Sorry might be a silly question with my lack of understanding..but will be very grateful for a reply.
Basically when I try to display "System.out.println(a1.name)" I am seeing null. Why is this?
The main problem here is that by redeclaring name in humano and neandarthal, you're shadowing the name field that exists in ape. That means that the objects created by humano and neandarthal have two name fields: The one defined by ape, and a separate one they define for themselves. Then in the code, you're being very inconsistent about which name field you use.
You almost certainly want to remove the name declarations in humano and neandarthal (and then use this., not super., on name), so you're using the field declared in ape.
The specific reasons for what you're seeing are:
The first line ("null"): humano assigns to its own name field, but when you use a1.name in your main, since a1 is declared as an ape, it's using the namefield in ape (which is still null, nothing has ever assigned to it).
The second line ("neandarthal"): neandarthal assigns to ape's name field (super.name = ...), not its own. Since a2 is declared as ape, a2.ape uses that and you see the string you assigned.
The third line ("I am a :humano"): humano's getBehavior uses its own name field, not ape's, so you see what the constructor assigned to it.
The fourth line ("I am a :null"): neandarthal's getBehavior uses its own name field, which is still null because nothing has ever assigned to it).
The fifth line ("ape"): I don't think you're confused about this, but humano's superclass is ape.
I can't figure out the way to save the supplierName value in a class object. I'm trying to change it in an addItem method using user input, and store it in a class object. It doesn't work. What am I doing wrong? Do I need to change my constructor? Or use a getter method?
Here is my code:
import java.util.Scanner;
public class PurchasedItem extends Item {
private String suppplierName;
public PurchasedItem() {}
#Override
public boolean addItem(Scanner input) {
super.addItem(input);
System.out.print("Enter the name of the supplier: ");
suppplierName = input.next();
return true;
}
#Override
public String toString() {
String str = super.toString();
return str + " Supplier: " + suppplierName;
}
}
A solution is to allow the constructor to take an argument such as:
public PurchasedItem(String constructorArgument){
supplierName = constructorArgument;
}
Then, you can do something like: PurchasedItem item = new PurchasedItem("some supplier");.
Since the class variable is private, you will also need getters/setters.
For instance:
Setter:
public void setSupplierName(String s){
this.supplierName = s;
}
Getter:
public String getSupplierName(){
return this.supplierName
}
Then to manipulate, you'd do something like:
item.setSupplierName("some company");
item.getsupplierName();
Though, you should be conscious of thread safety in regards to utilising setters.
I am struggling with making this code work. Here is my code. First class:
public class PersonalAccount extends Account{
private String cardNumber;
private String cardType;
public ArrayList<PersonalAccount> personalAccounts;
public int personal;
private PersonalAccount(String first, String last, String accountNumber, String cardNumber, String cardType){
super(first, last, accountNumber);
this.cardNumber = "";
this.cardType = "";
}
public void addPersonalAccount(PersonalAccount aPersonalAccount){
personalAccounts.add(aPersonalAccount);
}
public void getNumberOfPersonalAccounts(){
personal = personalAccounts.size();
}
public void listAccounts(){
for (PersonalAccount personalaccount : personalAccounts){
System.out.println("Personal Accounts");
System.out.println(personalaccount);
}
}
public void findAccount(){
int index = 0;
boolean found = false;
while(index < personalAccounts.size() && !found){
PersonalAccount personalaccount = personalAccounts.get(index);
if (personalaccount.getaccountNumber().equals(accountNumber)){
found = true;
}else{
index++;
}
}
}
}
When attempting to create an instance of this class in another class, it instead creates an instance of the PersonalAccount object. Is there a way around this issue? I am very new to Java and BlueJ it should be noted.
EDIT: sorry I should clarify. I'm trying to call the methods from this class in another class. But when declaring
PersonalAccount class1 = new PersonalAccount();
I get the error: constructor PersonalAccount in class PersonalAccount cannot be applied to given types.
I am trying to call the method on a button click (where numAcc is the button):
numAcc.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
int personal;
personal = class1.getNumberOfPersonalAccounts();
}
});
You dont have a default constructor so you cannot create a PersonalAccount like this:
PersonalAccount class1 = new PersonalAccount();
You have to pass the parameters first, last, accountNumber, cardNumber, cardType. It should be something like this:
PersonalAccount class1 = new PersonalAccount("FirstName", "Last_Name", "123456", "123456789", "Visa");
Read this: http://www.dummies.com/how-to/content/how-to-use-a-constructor-in-java.html
You don't have a zero-argument constructor for PersonalAccount which is why the given statement would fail.
Is that the problem you are having?
The problem is here that the constructor is private:
private PersonalAccount(String first, String last, String accountNumber, String cardNumber, String cardType)
Two things:
You need to change your constructor such that it is public so that it is accessible:
public PersonalAccount(String first, String last, String accountNumber, String cardNumber, String cardType)
The next thing is to supply parameters such as first, last, accountNumber etc. However, if you declare: public PersonalAccount(), then you would not need to supply arguments when you instantiate the class.
You should now be able to call the methods of this class!