Example of ClassName.method1Name().method2Name(); - java

Hi any one give me example of ClassName.method1Name().method2Name() what is this please give me simple java program example of this.I am confusing of this code.

ClassName.get1().get2().get3() is just a chain of method calls on successive classes. Imagine 3 objects:
class ClassName {
public static o1 get1() {
return new o1();
}
}
class o1 {
public o2 get2() {
return new o2();
}
}
class o2 {
public o3 method3() {
return new o3();
}
}
In each instance, the function call returned another object, which itself had a method to call, and so on and so forth.

Return current object reference from method call using return this;
public class ReturnClassType {
private String firstname;
private String lastname;
public ReturnClassType firstName(String firstname){
this.firstname = firstname;
return this;
}
public ReturnClassType lastName(String lastname){
this.lastname = lastname;
return this;
}
public String getName(){
return firstname + " " + lastname;
}
public static void main(String[] args) {
String Name = new ReturnClassType().firstName("Vicky").lastName("Thakor").getName();
System.out.println(Name);
}
}

Class Employee{
public Static List<Employee> list=new ArrayList<Employee>();
private String name;
Employee(){
public Employee(){
name = "";
}
public String getName(){
return name;
}
public void setName(String name){
this.name=name;
}
}
public static void main(String [] args){
Employee e = new Employee();
e.setname("test");
Employee e2 = new Employee();
e2.setname("test2");
Employee.list.add(e);
Employee.list.add(e2);
for(int i = 0 ; i < Employee.list.size(); i++){
System.out.println(Employee.list.get(i).getName());
}
}

Related

How to use BiConsumer to accept an object having an object, list of strings, and variables?

I am trying to use BiConsumer to accept an object that contains variables, an object and a list of strings in Java. I am not sure how to set the values into one object if using just BiConsumer. Maybe, if I tried to wrap Student object in a List and pass it into a new Student might help, but so far I get a null object. I haven't seen a lot of post with object containing just variables in one object and using BiConsumer.
#Test
public void testStudent() {
List<Object> objectList1 = new ArrayList<>();
Student student = new Student();
StudentLevel studentLevel = new StudentLevel("freshman", true);
List<String> studentLists = Arrays.asList("Maria", "Jose", "Juan");
Student student1 = new Student("Maria", "Lopez", "A", studentLevel, studentLists);
objectList1.add(student1);
BiConsumer<Object, List<Object>> biconsumer = (obj, list) -> {
for (Object object: list) {
// set and get but how?
// obj = object;
}
};
// To accept values from the object list see below for desired output
biconsumer.accept(student, objectList1);
// For debugging purpose
System.out.println("All Student: " + student);
}
public class Student {
private String name;
private String lastName;
private String grade;
private StudentLevel studentGrade;
private List<String> studentList;
public Student(final String name, final String lastName, final String grade, final StudentLevel studentGrade, final List<String> studentList) {
this.name = name;
this.lastName = lastName;
this.grade = grade;
this.studentGrade = studentGrade;
this.studentList = studentList;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getGrade() {
return grade;
}
public void setGrade(String grade) {
this.grade = grade;
}
public StudentLevel getStudentGrade() {
return studentGrade;
}
public void setStudentGrade(StudentLevel studentGrade) {
this.studentGrade = studentGrade;
}
public List<String> getStudentList() {
return studentList;
}
public void setStudentList(List<String> studentList) {
this.studentList = studentList;
}
}
public class StudentLevel {
private String level;
private Boolean pass;
public StudentLevel(final String level, final Boolean pass){
this.level = level;
this.pass = pass;
}
public String getLevel() {
return level;
}
public void setLevel(String level) {
this.level = level;
}
public Boolean getPass() {
return pass;
}
public void setPass(Boolean pass) {
this.pass = pass;
}
}
Desired output:
student = {Student#887}
name = "Maria"
lastName = "Lopez"
grade = "A"
studentGrade = {StudentLevel#889}
level = "freshman"
pass = {Boolean#906} true
studentList = {Arrays$ArrayList#890} size = 3
0 = "Maria"
1 = "Jose"
2 = "Juan"
You are assigning local reference of object to obj (won't copy the values)
obj = object; // means, student = object
no change will be reflected outside the scope of the consumer, instead, you need to modify the state using setters as:
((Student) obj).setName(((Student) object).getName());
obj = object; // after this point, student object won't be accessible in the current scope.
Note: You should have getters and setters to access private properties outside Student class, and this example just demonstrates the working by assigning name property.
Reference:
• Classes and Object

Generic linked list inside of linked list?

I am trying to write a program which stores information about a person in a linked list. I made a simple person class to store the name, age and addresses in the list. I would also like to store multiple addresses for EACH person, and a fact about the place in another linked list, inside the person class.
So for example, "Tara" can have a home address of "10 Central Ave" and a work address of "5 Willow street" etc. The problem is, I don't know how to have a linked list inside another.
My goal is to check whether the person's name is already on the list, and if so, add another address for them. (So that there is no repeats). I am a beginner and can really use some help.
public class Person {
private String name;
private int age;
public LinkedList <String> adresses;
public Person() {
name = "default";
age = 0;
adresses = new LinkedList<>();
}
public Person(String n, int a) {
name = n;
age = a;
}
public LinkedList<Adress> getAdresses() {
return adresses;
}
public void setAdresses(LinkedList<Adress> adresses) {
this.adresses = adresses;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String toString() {
return name+" "+age+" "+adresses;
}
}
public class Adress {
public String adress;
public String fact;
public Adress(String a, String f) {
adress = a;
fact = f;
}
public String getAdress() {
return adress;
}
public void setAdress(String adress) {
this.adress = adress;
}
public String getFact() {
return fact;
}
public void setFact(String fact) {
this.fact = fact;
}
}
public class Test {
public static void main(String[] args) {
Person Tara = new Person("Tara",35);
Person Judah = new Person("Judah",28);
Person Mark = new Person("Mark",45);
Person Seth = new Person("Seth",23);
LinkedList<Object> tester = new LinkedList<>();
tester.add(Tara);
tester.add(Judah);
tester.addLast(Mark);
tester.addLast(Seth);
System.out.println(tester);
}
}
How is about to use the next classic data structure for your project?
public class Person {
private String name
private int age;
public List<Address> addresses;
//...
}

Adding elements to an ArrayList from another class

I just have this basic code where I need help adding employee data to an ArrayList of another class. I am just writing this code in preparation for an assignment, so don't bash my code too much. Essentially though, i'll be needing to add elements of employees and delete them eventually. But for now, I just need help adding the elements to my other Employee class. =]
public class main {
private static Employee employee;
public static void main(String[] args) {
employee = new Employee(10,10);
System.out.println(employee.toString());
}
}
...............
import java.util.ArrayList;
public class Employee {
public int employeeNum;
public double hourRate;
ArrayList<Employee> Employee = new ArrayList<>();
public Employee(int employeeNum, double hourRate){
this.employeeNum = employeeNum;
this.hourRate = hourRate;
}
public String toString(){
return ""+employeeNum+hourRate;
}
}
Simple Example -
package com;
import java.util.ArrayList;
public class TestPage{
public static void main(String[] args){
Employee emp1, emp2;
emp1 = new Employee();
emp2 = new Employee();
emp1.setName("MAK");
emp2.setName("MICHELE");
emp1.setAddress("NY");
emp2.setAddress("WY");
//and keep putting other information like this
ArrayList<Employee> employee = new ArrayList<Employee>();
employee.add(emp1);
employee.add(emp2);
System.out.println("emp1 name is : " + employee.get(0).getName());
System.out.println("emp2 name is : " + employee.get(1).getName());
System.out.println("emp1 address is : " + employee.get(0).getAddress());
System.out.println("emp2 address is : " + employee.get(1).getAddress());
}
}
class Employee{
String name, address;
int age, salary;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
}
It seems like what you're asking is based on one employee having sub-employees and that structurally that probably represents a hierarchy (Some commenters seem to be missing that point). But that's an assumption on my part. Based on that assumption.
A little bit of feedback to start on structure of your main class:
public class main {
public static void main(String[] args) {
Employee employee = new Employee(10,10);
System.out.println(employee.toString());
}
}
It seems to me that there's no reason to have a static instance variable for that root employee instance. You should try to limit the scope of variables where possible. It seems like it could very well be in the main() method's scope.
public class Employee {
public int employeeNum;
public double hourRate;
ArrayList<Employee> employees= new ArrayList<>();
public Employee(int employeeNum, double hourRate){
this.employeeNum = employeeNum;
this.hourRate = hourRate;
}
public String toString(){
return ""+employeeNum+hourRate;
}
public ArrayList<Employee> getEmployees() {
return this.employees;
}
}
It may be better to name your arraylist employees or employeeList. I went with employees in this case because that convention is preferable.
And in relation to your question, ArrayList is pass by reference so you could just add a getter method for the sub-employee list (employees).
To add employees from your main method you could do something like
Employee rootEmployee = new Employee(5, 10.0);
rootEmployee.getEmployees().add(new Employee(6, 5.0));
Or you could add an additional method to Employee like this:
public void addEmployee(Employee e) {
employees.add(e);
}

How to add value from another class (java)

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

Output showing null values. how to resolve

i have three classes :
CustomerData.java
import java.util.Date;
public class CustomerData
{
private String FirstName;
private String LastName;
private int TaxId;
private String HomePhone;
private String WorkPhone;
private String HomeAddress;
private Date Dob;
private String EmployerName;
private boolean isEmployed;
private String ProDescription;
private String IsSameAsPrevious;
public String getFirstName() {
return FirstName;
}
public void setFirstName(String firstName) {
FirstName = firstName;
}
public String getLastName() {
return LastName;
}
public void setLastName(String lastName) {
LastName = lastName;
}
public int getTaxId() {
return TaxId;
}
public void setTaxId(int taxId) {
TaxId = taxId;
}
public String getHomePhone() {
return HomePhone;
}
public void setHomePhone(String homePhone) {
HomePhone = homePhone;
}
public String getWorkPhone() {
return WorkPhone;
}
public void setWorkPhone(String workPhone) {
WorkPhone = workPhone;
}
public String getHomeAddress() {
return HomeAddress;
}
public void setHomeAddress(String homeAddress) {
HomeAddress = homeAddress;
}
public Date getDob() {
return Dob;
}
public void setDob(Date dob) {
Dob = dob;
}
public String getEmployerName() {
return EmployerName;
}
public void setEmployerName(String employerName) {
EmployerName = employerName;
}
public boolean isEmployed() {
return isEmployed;
}
public void setEmployed(boolean isEmployed) {
this.isEmployed = isEmployed;
}
public String getProDescription() {
return ProDescription;
}
public void setProDescription(String proDescription) {
ProDescription = proDescription;
}
public String getIsSameAsPrevious() {
return IsSameAsPrevious;
}
public void setIsSameAsPrevious(String isSameAsPrevious) {
IsSameAsPrevious = isSameAsPrevious;
}
}
MainCntrlr.java
public class MainCntrlr {
public static void main(String[] args)
{
CustomerData customerData=new CustomerData();
customerData.setFirstName("RAVI");
customerData.setLastName("Shekhar");
customerData.setHomePhone("123456789");
customerData.setWorkPhone("1256554634");
customerData.setHomeAddress("Banagalore");
customerData.setEmployerName("ABc");
customerData.setProDescription("New Produtc");
customerData.setTaxId(1233434343);
ContrOne ctr=new ContrOne();
ctr.displayInformation();
}
}
ContrOne.java
import com.blr.CustomerData;
public class ContrOne
{
public void displayInformation()
{
CustomerData cd=new CustomerData();
System.out.println("displaying customer Info");
System.out.println(cd.getFirstName());
System.out.println(cd.getLastName());
System.out.println(cd.getHomePhone());
System.out.println(cd.getWorkPhone());
System.out.println(cd.getEmployerName());
System.out.println(cd.getProDescription());
System.out.println(cd.getDob());
System.out.println(cd.getTaxId());
}
}
Output is :
displaying customer Info
null
null
null
null
null
null
null
0
In your method ContrOne.displayInformation() you create a new new CustomerData() object. Thus it is not properly initialized. Maybe you want to pass customerData from your main method to displayInformation?
You are creating a CustomerData object in MainCntrlr class but not passing it to the ContrOne class. ContrOne is creating it's own CustomerData object and printing it's values which are null.
A possible solution could be to change the signature of displayInfo and pass the CustomerData object to this method from MainCntrlr class.
A few other points to note
The class names are confusing. It is not clear why you have created MainCntrlr and ContrOne classes separately.
If all you want is to display Customer information, you may want to override the toString() method in CustomerData class instead.
Your ContrOne.displayInformation() instantiates a new CustomerData() object to cd but never populates fields in it.
If you want your ContrOne to display information, rather do this:
public class ContrOne {
public void displayInformation(CustomerData cd) {
System.out.println("displaying customer Info");
System.out.println(cd.getFirstName());
System.out.println(cd.getLastName());
System.out.println(cd.getHomePhone());
System.out.println(cd.getWorkPhone());
System.out.println(cd.getEmployerName());
System.out.println(cd.getProDescription());
System.out.println(cd.getDob());
System.out.println(cd.getTaxId());
}
}
and in MainCntrlr
public static void main(String[] args) {
CustomerData customerData=new CustomerData();
customerData.setFirstName("RAVI");
customerData.setLastName("Shekhar");
customerData.setHomePhone("123456789");
customerData.setWorkPhone("1256554634");
customerData.setHomeAddress("Banagalore");
customerData.setEmployerName("ABc");
customerData.setProDescription("New Produtc");
customerData.setTaxId(1233434343);
ContrOne ctr=new ContrOne();
ctr.displayInformation(customerData);
}

Categories