Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Hello so I'm having a bit of difficulties with a setter method on objects.
public class Company {
private String companyName;
public static int numberOfEmployees;
public Employees employees[];
public void setEmployees( String name, String heritage, String [] programmingLanguages, Salary d) {
Employees employee1 = new Programmer(name, heritage,programmingLanguages, d,d.getBasicBrutoSalary());
employees[numberOfEmployees] = employee1;
numberOfEmployees++;
}
So basicly this is a method defined in the 'company class' while making an Employees object who's using the parameters for making a 'Programmer'.
But that's not the deal, what I want tot do is by calling this setter method, automaticly create an object. So each time it's used, kind of increment the name of the object it's going to make.
So for example the first time I use it it makes an object called Employee1 and stores it in Employee[0].. second time I want it to store Employee2 into Employee[1].
Maybe I'm making this way too difficult but I'm just trying things out, and can't seem to find a way to make this work.
I suppose that Programmer object is subclass of Employees, or else it will not work. More or less it should look like the following:
public class Company {
private String companyName;
public static int numberOfEmployees;
public static Employees employees[];
public void setEmployees( String name, String heritage, String [] programmingLanguages, Salary d) {
numberOfEmployees++;
employees[numberOfEmployees] = new Programmer(name, heritage,programmingLanguages, d,d.getBasicBrutoSalary());;
}
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have a List<Person>. Person's attributes are String name, String secondName, int phoneNum.
How can I sort this List by the String property secondName?
I've tried moving into another List<String> the data from the previous list moving secondName to the first position, then applying Collection.sort and creating other List<Person> with the data of the List sorted but its too complicated.
Java 8 introduced a sort method in the List interface which can use a comparator.
If you have a getter for secondName you can do this:
myList.sort(Comparator.comparing(Person::getSecondName));
If you don't have a getter use this one:
myList.sort(Comparator.comparing((person)->(person.secondName)));
for more ways and answers:
link
Lets assume here is your Pojo Class Person.
class Person{
String name;
String secondName;
//Getter and setters
}
Then sort the list by java collection API itself.
java.util.Collections.sort(personList, new Comparator<Person>() {
#Override
public int compare(Person o1, Person o2) {
return o1.getSecondName().compareToIgnoreCase(o2.getSecondName());
}
});
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I'm new in Java and I'm trying to create a social network. I have a array of objects "People" which their names, e-mails and so on. I would like to create relations between them (like 'friends') so that program would connect two object.
I though about doing another array inside of every "Person" object, so each person would have a name, e-mail and friend array with all friends inside of it. I don't know how to refer to an other object of type People inside of object of this type.
I don't know if it's clear..
Hope it is!
Thank You in advance!
Here's one way:
public class Person {
private String name;
private String email;
private List<Person> friends;
// Leave the rest for you.
public void addFriend(Person p) {
if (p != null) {
this.friends.add(p);
}
}
public void removeFriend(Person p) {
this.friends.remove(p);
}
public boolean isFriend(Person p) {
return this.friends.contains(p);
}
}
You'll want constructors and a way to add and remove Person from your friend List.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I'm looking for a way in java (not sql), to store a few inputs. And be able to refer each entry.
So for example the Id of person entering info, date entered, a score of some kind (int).
All I can find is a multi-dimensional array.
A potential solution is to create a class for the data you want, for example:
public class Data {
private int score;
private String info, date;
public Data(String info, String date, int score) {
this.score = score;
this.info = info;
this.data = data;
}
}
Now you can create these objects with the data that the user inputs, and add them to a collection (i.e an ArrayList) or simply to an array. Here's an example with an array:
Data[] myData = new Data[4]; // 4 is an arbitrary maximum number of entries
// Get the data into variables
// Now create a new Data object with the information and add it to the array
myData[0] = new Data(info, date, score);
// Repeat this process for all the input
What you are looking for can be done in many ways, the most accessible and understandable one probably being a List or ArrayList.
You seem to want to keep a list of Players, each having a few attributes. You could start by creating a Player object, like so
Class Player {
Public Player(int score, String name) {
this.score = score;
this.name = name;
}
private int score;
private String name;
public void setScore(int score){
this.score = score;
}
public int getScore(){
return this.score;
}
// Repeat for all class variables
Then, you would declare an ArrayList in one of your other classes. Note that it has the parameter type Player.
ArrayList<Player> playerList = new ArrayList<Player>();
You can then add Player objects to the playerList like so:
Player p = new Player(345, "randomName");
playerList.add(p);
If you then want to access one of your Players, and one of their scores specifically, you would do this:
int currentPlayerScore = playerList.get(0).getScore();
Naturally, 0 here stands for the number of the ArrayList entry.
Good luck.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Im a new programmer and I want to know when its best practice to use overloaded constructors and what makes it different from single primary constructor.
The short answer is: you should use overloading whenever you need it.
As a real-life example, take a look at the JLabel API: https://docs.oracle.com/javase/8/docs/api/javax/swing/JLabel.html
JLabel has quite a few constructors: one that just takes a String, one that takes a String and an icon, one that only takes an icon, and one that doesn't take any arguments at all.
You would use each constructor when you want to construct that kind of JLabel: one that displays a String, one that displays a String and an icon, one that only displays an icon, or one that doesn't display anything yet (until you call one of its setter functions).
Constructor overloading is useful when you want to allow user to create objects in multiple different ways.For example to be able to create a simple Student class object in following different ways:
new Student(45); // given student id
new Student("name"); // given student name
new Student(45,"name"); // given student registration id and name
This helps ease the task of creating objects according to our requirements. You can link this concept with various java API's as the provide a no of different ways to initialize an object of a class.
Also you can combine the Construstor Overloading with Constructor chaining.
Here is an examlple:
public Student (int id){
this(id,"ANY-DEFAULT-NAME"); // calls the constructor of same class with 2 params
}
public Student (String name){
this(ANY-DEFAULT-ID,name);// calls the constructor of same class with 2 params
}
public Student (int id,String name){
// here you can initialize the instance variables of the class.
}
You can overload a constructor based in your needs. For example,let's say that you have a simple class called Dog, that have some attributes like: Name,Breed, Birthday, Owner and skin color.
public class Dog {
private String name;
private String breed;
private Date birthday;
private String owner;
private String skinColor;
/*Getters and Setters*/
...
}
If you instance a object of type Dog and want to set a all or some of the attributes' values, you'll have to call all the setters methods of the object, but with the constructor, you can save that step passing the values directly every moment you instance the object.
Example:
public class Dog {
private String name;
private String breed;
private Date birthday;
private String owner;
private String skinColor;
public Dog(String name, String breed,Date birthday,String owner,String skinColor){
this.name = name;
this.breed = breed;
this.birthday = birthday;
this.owner = owner;
this.skinColor = skinColor;
}
/*Getters and Setters*/
...
}
Dog myDog = new Dog("Jose", "Chiguagua",new Date(),"Jhon","Brown");
If you want only instance the object with the name only, you can do it too. A good practice is, if you have an object with attributes that is necessary to fill in some point, provide the default constructor, if you do not provide it, you will always need to pass some values for instance a object. This give flexibility to the programmer.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have created an ArrayList which contains some infos .
Every line has this pattern : name age sex job .
I'm searching for a way to take for example the every value separately and to assign them in different variables. I've search the web , but I came out to nothing ! So if there is someone that could help me I would appreciate it !
I have no idea how to do it so I can't provide code ! Soory I'm newbie in java .
Create a class Person that will hold your data:
class Person {
private String name;
private int age;
private String sex;
private String job;
//class constructor...
//getters and setters...
}
Then, read the file. For every line in the file, create an instance of Person class and store it in a List. I'll do this in pseudocode, it's up to you the concrete implementation (otherwise it would be me doing your homework =\):
List<Person> people <- new ArrayList<Person>()
open_file(theFile)
while not_end_of_file
String name <- read_text
String age <- read_int
String sex <- read_text
String job <- read_text
Person person <- new Person()
person->setName(name)
//similar for other fields...
people->add(person)
end while
Sounds like you have a List of Strings. If so, you could call the String split(String regex) method to get a String[] back.
I think what you need to do is create a Person object which would contain a name, age, sex and job.
Then you could create an Arraylist of type Person.
List<Person> people = new ArrayList<Person>();
The person class would look something like this:
public class Person {
String name, sex, jobTitle;
int age;
public Person(String name){
this.name = name;
}
}