Click here to Skip to main content
15,789,776 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How do I store the user defined values into the objects?
Where do I supposed to insert the sorting algorithm?

What I have tried:

package Question1;

import java.util.Scanner;
import java.util.*;

public class Question1 {
private String Name;
private int Salary;
private int Age;
private String Department;

void setName(String Name) //setter and getter methods
{
this.Name =Name;}
String getName()
{
return Name;}

void setSalary(int Salary){
this.Salary = Salary;
}
int getSalaray(){
return Salary;}

void setAge(int Age){
this.Age = Age;}
int getAge()
{
return Age;}

void setDepartment(String Department){
this.Department= Department;}
String getDepartment()
{
return Department;}

Question1(String Name, int Age, int Salary, String Department) //constructor
{
this.Name =Name;
this.Age = Age;
this.Salary = Salary;
this.Department = Department;

}   
    public static void main(String[] args) {
        for (int i=0; i<=4; i++){
        Scanner s = new Scanner(System.in);
        System.out.print("Enter Name:");
        String Name =s.nextLine();
        
        Scanner s1 = new Scanner(System.in);
        System.out.print("Enter Salary:");
        int Salary = s1.nextInt();
        
        Scanner s2 = new Scanner(System.in);
        System.out.print("Enter Age:");
        int Age = s2.nextInt();
        
        Scanner s3 = new Scanner(System.in);
        System.out.print("Enter Department:");
        String Department = s3.nextLine();
        System.out.println("\n");
    }

    }
        
    }
Posted
Updated 17-Sep-17 23:31pm

Quote:
How do I store the user defined values into the objects?
In your Main method you are collecting values for initializing 4 instance of the Question1 (rather infortunate name, Employee could be possibly better) class.
So, create an array of Question1 objects and initialize such objects with the values collected from the user.


Then write the sorting method (could be, for instance a static method of the Question1 class or a standard method of a unrelated class), accepting, as parameter, an array of Question1 objects.
 
Share this answer
 
Comments
Member 13390616 19-Sep-17 5:00am    
package Question1;
import java.util.Scanner;
import java.util.*;

public class Question1 {
private String Name;
private int Salary;
private int Age;
private String Department;



void setName(String Name) //setter and getter methods
{
this.Name =Name;}
String getName()
{
return Name;}

void setSalary(int Salary){
this.Salary = Salary;
}
int getSalaray(){
return Salary;}

void setAge(int Age){
this.Age = Age;}
int getAge()
{
return Age;}

void setDepartment(String Department){
this.Department= Department;}
String getDepartment()
{
return Department;}

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);
Question1[] array = new Question1[4];
String temp,c;
int a,b;
for(int i=0; i<=array.length; i++){

System.out.print("Enter Name:");
temp = sc.nextLine();
array[i] = new Question1();
array[i].setName(temp);

System.out.print("Enter Age:");
a = sc.nextInt();
array[i].setAge(a);

System.out.print("Enter Salary:");
b = sc.nextInt();
array[i].setSalary(b);

System.out.print("Enter Department:");
c = sc.nextLine();
array[i].setDepartment(c);

System.out.println("\n");
}
}

}

I tried this. Where should I insert the sorting algorithm?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900