I have this :
public class NewClass {
String DNI;
String name;
boolean asist [][]=new boolean[3][4];
public NewClass(String DNI, String name) {
this.DNI = DNI;
this.name = name;
for (int i=0;i<asist.length;i++)
{
for(int j=0;j<asist[0].length;j++)
{
asist[i][j]=true;
}
}
}
public String getDNI() {
return DNI;
}
public void setDNI(String DNI) {
this.DNI = DNI;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean[][] getAsist() {
return asist;
}
public void setAsist(boolean[][] asist) {
this.asist = asist;
}
and I saved this code in HashTable < String,Object> has=new HashTable();.
Ok then I want to set asist[0][0] and i dont know how to do this I tryed this:
has.get(key).setAsistencia([0][0]false)
Thanks and sorry for my bad english
You were pretty close.
First you need to retrieve the array and then set the respective part of it to false.
has.get(key).getAsist()[0][0] = false;
Related
We have an activity that will store and display the information of an employee.
I've already created no-modifier class named Person and a public class named Employee which is the the main method. My problem is I don't know how to make the boolean in the class Person which will be used in the main method with a scanner.
class Person {
private String name;
private int contactNum;
private boolean status;
public boolean isRegular;
public String getName(){
return name;
}
public int getContactNum(){
return contactNum;
}
public void setName(String name){
this.name=name;
}
public void setContactNum(int contactNum){
this.contactNum=contactNum;
//how to make the boolean of status and isRegular?
}
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Person P = new Person();
System.out.println("Type employee's name, contact number");
System.out.println("Press Enter after every input");
P.setName(input.nextLine());
P.setContactNum(input.nextInt());
System.out.println("Press Y if employee is regular or N if not");
//how to use boolean here that comes from the class Person?
System.out.println("Name: "+ P.getName());
System.out.println("Contact Number: "+ P.getContactNum());
System.out.println("Status:" + this is where the user is ask to Press Y if employee is regular or N if not )//the status is if the employee is regular or not.
My suggestion for your code:
System.out.println("Press Y if employee is regular or any other key if not");
P.setRegular(input.nextLine().equalsIgnoreCase("Y"));
Your constructor
public class Person {
private String name;
private int contactNum;
private boolean status;
private boolean isRegular;
//No arg constructor
public Person() {
}
//Full arg constructor
public Person(String name, int contactNum, boolean status, boolean isRegular) {
this.name = name;
this.contactNum = contactNum;
this.status = status;
this.isRegular = isRegular;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getContactNum() {
return contactNum;
}
public void setContactNum(int contactNum) {
this.contactNum = contactNum;
}
public boolean isStatus() {
return status;
}
public void setStatus(boolean status) {
this.status = status;
}
public boolean isRegular() {
return isRegular;
}
public void setRegular(boolean regular) {
isRegular = regular;
}
}
Edit
I've noticed an error in the above code and fixed it. This line should be:
P.setRegular(input.next().equalsIgnoreCase("Y"));
You can print booleans as is just like any other Java primitive. `
System.out.println("Status:" + P.isRegular());
would print Status: true or Status: false.
If you want it to print Status: Yes or Status: No, you could do something like this:
System.out.println("Status: ".concat((P.isRegular())?("Yes"):("No")));
Helper Class
public class HomeScreenChatsHelper implements Comparable {
private String ID;
private String Name;
private String Image;
private String From;
private String Seen;
private String LastMessage;
private String LastMessageTime;
public HomeScreenChatsHelper(){
}
public HomeScreenChatsHelper(String id, String name, String image, String from, String seen, String lastmessage, String lastMessageTime) {
this.ID=id;
this.Name = name;
this.Image = image;
this.From = from;
this.Seen = seen;
this.LastMessage = lastmessage;
this.LastMessageTime = lastMessageTime;
}
public String getID() {
return ID;
}
public void setID(String id) {
ID = id;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getImage() {
return Image;
}
public void setImage(String image) {
Image = image;
}
public String getMessage() {
return LastMessage;
}
public void setMessage(String message) {
LastMessage = message;
}
public String getTime() {
return LastMessageTime;
}
public void setTime(String time) {
LastMessageTime = time;
}
public String getFrom() {
return From;
}
public void setFrom(String from) {
From = from;
}
public String getSeen() {
return Seen;
}
public void setSeen(String seen) {
Seen = seen;
}
#RequiresApi(api = Build.VERSION_CODES.KITKAT)
#Override
public int compareTo(Object comparestu) {
long compareage= Long.parseLong(((HomeScreenChatsHelper)comparestu).getTime());
long a = Long.parseLong(LastMessageTime);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
}
return Long.compare(a,compareage);
}
#Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof HomeScreenChatsHelper)) return false;
HomeScreenChatsHelper that = (HomeScreenChatsHelper) o;
return getID().equals(that.getID());
}
#Override
public int hashCode() {
return getID().hashCode();
}
Activity
for(HomeScreenChatsHelper str : mChats) {
if (str.getID().equals(ID)) {
mChats.remove(ID);
break;
}
}
There are a ton of tutorials on how to do it and I've spent the past week looking for a solution and I still don't have it. Is there anyway I can remove an whole object by just specifying just the ID? I wont have the values of all the other fields so I just want to remove a particular object by its ID. Also I cant use the clear option because I need the other data. So can someone help me out please?
With the present code nothing happens. No errors but doesn't work
By using java-8 you can filter the list, result will be the List<HomeScreenChatsHelper> that does have HomeScreenChatsHelper with same id
List<HomeScreenChatsHelper> mChats = new ArrayList<>();
//filter
List<HomeScreenChatsHelper> result = mChats.stream()
.filter(str->!str.getId().equals(Id)).
.collect(Collectors.toList());
Or by using Iterator
// Iterator.remove()
Iterator itr = mChats.iterator();
while (itr.hasNext())
{
HomeScreenChatsHelper x = itr.next();
if (x.getId().equals(Id)) }
itr.remove();
}
}
Your question is quite unclear. is mChats a List containing HomeScreenChatsHelper objects?
I assume so. If this is the case, then you can change your foreach loop into the normal loop
//Assuming mChats is List e.g ArrayList
for (int i = 0; mChats.size(); i++){
if (mChats.get(i).getID().equals(ID)) {
mChats.remove(i);
break;
}
}
The easiest way in Java 8 or later is with Collection#removeIf:
mChats.removeIf(str -> str.getID().equals(ID));
By the way, the convention in Java is for fields to begin with a lowercase letter.
I am making a text-based game on JavaFX, and after I hit a tree, I want to get Oak logs.
I have already built my inventory, and I have put default items in it such as Water, Bread, etc.
I am trying to add my Oak Logs to my inventory, but nothing is working.
Here is a part of my code:
Item ItemList[] = {new Bread(), new OakLog()};
Optional<ButtonType> result = alert.showAndWait();
if(result.get() == buttonTypeOak) {
woodcuttingXP = woodcuttingXP + oakXP;
dialogue.appendText("You swing at an Oak tree. + " + oakXP + "XP.\n");
dialogue.appendText("You gathered 1 log.\n");
mainCharacter.getInventory().add(new OakLog());
}
Here is my Item Class:
package game;
public class Item {
private String name;
private int weight;
private int quantity;
private int value;
private String description;
public Item(String name, int value, String description) {
this.name = name;
this.value = value;
this.description = description;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String toString() {
return getName();
}
}
And finally, here is my Character class:
package game;
import java.util.ArrayList;
import beverages.Water;
import items.OakLog;
import rawFood.Bread;
public class Character {
private String name;
private int hydrationLevel;
private int healthLevel;
private int hungerLevel;
private int woodcuttingLevel;
public int getWoodcuttingLevel() {
return woodcuttingLevel;
}
public void setWoodcuttingLevel(int woodcuttingLevel) {
this.woodcuttingLevel = woodcuttingLevel;
}
public int getHungerLevel() {
return hungerLevel;
}
public void setHungerLevel(int hungerLevel) {
this.hungerLevel = hungerLevel;
}
private ArrayList<Item> inventory = new ArrayList<Item>();
public ArrayList<Item> getInventory() {
return inventory;
}
public void setInventory(ArrayList<Item> inventory) {
this.inventory = inventory;
}
//creates a person with two basic items
public Character(String name){
this.name = name;
this.hydrationLevel = 100;
this.healthLevel = 100;
this.hungerLevel = 100;
this.woodcuttingLevel = 1;
addToInventory (new Bread());
addToInventory (new OakLog());
addToInventory (new Water());
}
//GETTERS AND SETTERS
public String getName() {
return name;
}
public int getHydrationLevel() {
return hydrationLevel;
}
public void setHydrationLevel(int hydrationLevel) {
this.hydrationLevel = hydrationLevel;
}
public int getHealthLevel() {
return healthLevel;
}
public void setHealthLevel(int healthLevel) {
this.healthLevel = healthLevel;
}
//END GETTERS AND SETTERS
/*Method Name: eat()
*Method Inputs: a piece of food
*Method Purpose: Will allow the user to eat food
*/
public Item getItemFromInventory(int index){
Item item = inventory.get(index);
return item;
}
public void addToInventory(Item item){
if(inventory.contains(item)){
item.setQuantity(item.getQuantity()+1);
}
else{
item.setQuantity(1);
inventory.add(item);
}
}
public String toString(){
return "Character Stats:\nName:" + getName() + "\nHydration: " + getHydrationLevel() + "\nHealth: " + getHealthLevel() + "\nWoodcutting: " + getWoodcuttingLevel();
}
}
In your code, you have:
if(inventory.contains(item)){
item.setQuantity(item.getQuantity()+1);
}
This just updates the quantity of the local variable item in the method, not the item in the inventory.
I have a java bean class such as :
public class EncBean {
private String name;
private String ReversedBinary;
private String ConcatenatedData;
public String getReversedBinary() {
return ReversedBinary;
}
public void setReversedBinary(String ReversedBinary) {
this.ReversedBinary = ReversedBinary;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getConcatenatedData() {
return ConcatenatedData;
}
public void setConcatenatedData(String name) {
this.ConcatenatedData = ConcatenatedData;
}
}
and I tried to set a value for the private java bean field(ConcatenatedData) as follow :
public EncBean conctdat(){
EncBean encBean4 = new EncBean();
encBean4.setConcatenatedData(inputkey.concat(var));
return encBean4;
}
and in main i tried to access this value as:
mainenc concatdata =new mainenc();
EncBean encbeandata = concatdata.conctdat();
System.out.println("concatenated data is: "+encbeandata.getConcatenatedData());
but it gives me null
concatenated data is: null
You should fix implementation of setConcatenatedData() to :
public void setConcatenatedData(String name) {
this.ConcatenatedData = name; // instead of this.ConcatenatedData = ConcatenatedData
}
The first one is:
public void setConcatenatedData(String name) {
this.ConcatenatedData = name;
}
Second one, you should double check if inputkey.concat(var) is null.
You can do it by Getter & Setter.
This will help:
setConcatenatedData(String name) {
this.ConcatenatedData = name;
}
I have two classes: profesor and subject
public class Profesor {
private int numbClassroom;
public Profesor(int numbClassroom) {
this.numbClassroom = numbClassroom;
}
public int getNumbClassroom() {
return numbClassroom;
}
public void setNumbClassroom(int numbClassroom) {
this.numbClassroom = numbClassroom;
}
public String ToString(){
return "Number of classroom: "+numbClassroom;
} }
The second class is:
public class Subject{
String name;
Profesor lecturer = new Profesor();
Date yearOfStudy;
public void Dodeli(Profesor p){
??????
}}
I do not know how to add professor like a lecturer to a current subject
Like this? I don't see any problem.
public void Dodeli(Profesor p){
lecturer = p;
}
Profesor lecturer = new Profesor();
No need to instantiate lecturer. Just declare it. Then have getter/setter methods for it
Then you can assign Professor to Subject
Subject subj = new Subject("OOP"); //assuming you have corresponding constructor
subj.setLecturer(new Professor()); //or if you have existing prof object
Maybe require something like this : try to encapsulate your code
public class Professor {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class Subject{
private String name;
private Professor professor;
private int numbClassroom;
private Date yearOfStudy;
public int getNumbClassroom() {
return numbClassroom;
}
public void setNumbClassroom(int numbClassroom) {
this.numbClassroom = numbClassroom;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Professor getProfesor() {
return professor;
}
public void setProfesor(Professor profesor) {
this.professor = profesor;
}
public void Dodeli(){
System.out.println("Pofessor "+getProfesor().getName()+" is teaching "+getName()+" in Room NO :"+getNumbClassroom());
}
}
public class TestImpl {
public static void main(String arr[])
{
Subject subject = new Subject();
Professor professor = new Professor();
subject.setName("Biology");
professor.setName("MR.X");
subject.setNumbClassroom(1111);
subject.setProfesor(professor);
subject.Dodeli();
}
}