In Java programming Question 4: Create a class Developer with fields: age, salar
ID: 3920659 • Letter: I
Question
In Java programming Question 4:
Create a class Developer with fields: age, salary, first name, last name and id. Add
two constructors (no argument constructor and constructor with parameters to initialize the object fields age, salary, first name, last name and id).
regular methods getAge(), setAge(age), getSalary(), setSalary(Salary), getName(), setName(firstName, Lastname) and getID(), setID(id).
A static field that contains total number of Developers
A static method that prints total number of Developers
Write the main method to test all these functionalities
Explanation / Answer
Developer.java
public class Developer {
//Declaring instance variables
private int id;
private int age;
private double salary;
private String fname;
private String lname;
//Declaring Static variable
private static int cnt = 0;
//Zero argumented constructor
public Developer() {
cnt++;
}
//Parameterized constructor
public Developer(int id, int age, double salary, String fname, String lname) {
this.id = id;
this.age = age;
this.salary = salary;
this.fname = fname;
this.lname = lname;
cnt++;
}
// getters and setters
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public String getFname() {
return fname;
}
public void setFname(String fname) {
this.fname = fname;
}
public String getLname() {
return lname;
}
public void setLname(String lname) {
this.lname = lname;
}
public static void displayTotDevelopers() {
System.out.println("Total no of developers :" + cnt);
}
}
_____________________
Test.java
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
//Declaring variables
int id;
int age;
double salary;
String fname;
String lname;
/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
Scanner sc=new Scanner(System.in);
//Getting the input entered by the user
System.out.print("Enter id :");
id=sc.nextInt();
System.out.print("Enter Age :");
age=sc.nextInt();
System.out.print("Enter First Name :");
fname=sc.next();
System.out.print("Enter Last Name :");
lname=sc.next();
System.out.print("Enter Salary :");
salary=sc.nextDouble();
//Creating an instance of Developer class
Developer d=new Developer(id, age, salary, fname, lname);
//Displaying the Developer info
System.out.println(" Displaying the Developer Info :");
System.out.println(" Id :"+d.getId());
System.out.println("Age :"+d.getAge());
System.out.println("First Name :"+d.getFname());
System.out.println("Last Name :"+d.getLname());
System.out.println("Salary :"+d.getSalary());
Developer.displayTotDevelopers();
}
}
_________________
Output:
Enter id :111
Enter Age :24
Enter First Name :Muhammad
Enter Last Name :Ali
Enter Salary :50000.00
Displaying the Developer Info :
Id :111
Age :24
First Name :Muhammad
Last Name :Ali
Salary :50000.0
Total no of developers :1
_______________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.