Java performing code only from constructor's subclass - java

Is it possible to protect from performing code placed in superclass constructor? In this example the output is
From Person
From Student
but I don't need to print out From Person. If I delete super(a, n); then program will not compile. Is it possible to print out only message from subclass?
class Person {
private int age;
private String name;
public Person(int a, String n) {
this.age = a;
this.name = n;
System.out.println("From Person");
}
public int getAge() {
return age;
}
public void setAge(int a) {
this.age = a;
}
public String getName() {
return name;
}
public void setName(String n) {
this.name = n;
}
}
class Student extends Person {
private String specialization;
public Student(int a, String n, String s) {
super(a, n);
specialization = s;
System.out.println("From Student");
}
public String getSpecialization() {
return specialization;
}
public void setSpecialization(String s) {
this.specialization = s;
}
}
public class Classes {
public static void main(String[] args) {
Student student_Jack = new Student(20, "Jack", "IT");
}
}

Instead of using super(a,n) you should use your methods for setting those variables that you have made.
public Student(int a, String n, String s) {
setAge(a);
setName(n);
specialization = s;
System.out.println("From Student");
}
You must also add an empty constructor in person.
public Person(){}
This will give you the same functionality, without needing to call the super constructor.

Related

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;
//...
}

Sending a List of different objects and returning the sum of their properties

I have to classes
public class Consumer{
private String name;
private int salary;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
}
and next
public class Donor {
private String name;
private int amount;
private String location;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAmount() {
return amount;
}
public void setAmount(int amount) {
this.amount = amount;
}
}
now i have another class which contains a method method1()
public class GenericClass<T> {
public void method1(List<T> list){
Iterator i = list.iterator();
while (i.hasNext()){
}
}
}
and My main method is
public class MainMethod {
public static void main(String[] args) {
List<Donor> d = new ArrayList<>();
Donor donor = new Donor();
donor.setAmount(500);
donor.setName("bill");
Donor donor1 = new Donor();
donor.setAmount(1250);
donor.setName("linda");
d.add(donor);
d.add(donor1);
GenericClass genericClass = new GenericClass();
genericClass.method1(d);
}
}
i want to make this method1() dynamic and return a dynamic result.
so if i send the list of Consumer then it should return me the sum of all salaries and if i send the list of Donor then it should send me the sum of amount donated ?
how can this be achieved ?
First, you'd probably not make the class GenericClass generic but the method method1().
Then you could provide a ToIntFunction<T> which takes an object of type T and returns an int value. Thus your method could look like this (Java8 code):
public <T> int method1(List<T> list, ToIntFunction<T> transformation){
return list.stream().collect(Collectors.summingInt(transformation));
}
You'd then call that method like this:
int sumSalaries = method1(consumers, Consumer::getSalary);
int sumDonations = method1(donors, Donor::getAmount);
Pre-Java8 code would be possible as well but it would be a little bigger (you'd need to provide ToIntFunction, implementations of that interface and a slightly larger method body).
Alternatively you could use an Interface that's implemented by both classes but that would require you to use a common method name (e.g. getAmount() or getIntValue() etc.)

How to acces a void method from another class?

how do I call a void method from another class to a new class with main?
I have two classes, but I don't see the error I am making.
public class Person {
private int age;
private String name;
public Person(int a, String n) {
a = age;
n = name;
}
public void printInfo() {
System.out.println(age + name);
}
//
public class Main {
public static void main(String[] args) {
Person obj1 = new Person(22, "Dan");
obj1.printInfo();
}
}
EDIT: Move the main method to a different class and done.
TestPerson.java
public class TestPerson {
public static void main(String[] args) {
Person obj1 = new Person(22, "Dan");
obj1.printInfo();
}
}
You have few mistakes in your code. It should be like as follows :
public class Person {
private int age;
private String name;
//Your constructor was wrong
public Person(int a, String n) {
age = a;
name = n;
}
public void printInfo() {
System.out.println(age + name);
}
}

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

Constructors and Keyword 'this'

I have a syntax error and I don't Know how to fix it. The code appears correct to me, but Eclipse is telling me that "Constructor call must be the first statement in a
constructor" at the methods setName() and setAge()
public class KeywordThis {
private String name;
private int age;
public KeywordThis(){
this.name = "NULL";
this.age = 0;
}
public KeywordThis(String s, int a){
this.name = s;
this.age = a;
}
public KeywordThis(String s){
this.name = s;
}
public KeywordThis(int a){
this.age = a;
}
public int setAge(int a){
this(a);
}
public String setName(String s){
this(s);
}
public static void main(String args[] ){
}
}
You cannot call a constructor like that from an instance method. You want your setter to change the value of the object you already have, not create a new one. I think you mean to do this:
public void setAge(int a){
this.age = a;
}
public void setName(String s){
this.name = s;
}
Also note that your setters don't usually return values, so I've changed them to return type void.
Once an object has been created you can not manually call the constructor. Constructors can only be called inside another constructor.
As others have pointed out it should be:
public void setAge(int a) {
this.a = a;
}
As a note, your setters should look like
public void setAge(a) {
this.a = a;
}
and not construct a new object. If you don't do this, you are breaking an ubiqutous Java convention.
Assuming you want to create a new instance in a setter, you would do something like
public KeywordThis setAge(a){
return new KeywordThis(a);
}
and not use this(a). Using this as you are attempting should only be done in a constructor (to invoke another constructor for the same class).
public class KeywordThis {
private String name;
private int age;
public KeywordThis(){
this.name = "NULL";
this.age = 0;
}
public KeywordThis(String s, int a){
this.name = s;
this.age = a;
}
public KeywordThis(String s){
this.name = s;
}
public KeywordThis(int a){
this.age = a;
}
public int setAge(int a){
this(a);
int b=a;
return b;
}
public String setName(String s){
this(s);
String s1=s;
return s;
}
public static void main(String args[] ){
KeywordThis ob1=new Keyword();
ob1.setAge(20);
ob1.setName("sandy");
}
}
java
share|edit

Categories