In: Computer Science
Java Code:
Write an application that takes in user input. (Name, Age, and Salary)
------
Write an application that includes a constructor, user input, and operators.
Two programs are provided below. Both programs satisfy the conditions you have given in question. Both programs are explained in code comments . Output screenshot is also provided. If you need any further clarification please ask in comments.
###############################################################################
CODE 1
import java.util.Scanner; //importing to use scanner for taking input
public class Employee {
//main method
public static void main(String[] args) {
//variables to hold values
String name;
int age;
double salary;
//creating object of Scanner to take input from user through System standard input
Scanner scan=new Scanner(System.in);
System.out.println("Enter name: ");
name=scan.nextLine(); //taking string input through scanner nextLine()
System.out.println("Enter age:");
age=scan.nextInt(); //taking integer input using scanner nextInt()
System.out.println("Enter salary:");
salary=scan.nextDouble(); //taking double input using scanner nextDouble()
//displaying all the variables data
System.out.println("Name: "+ name +" Age: "+ age +" Salary: "+salary);
scan.close(); //closing scanner
}
}
OUTPUT
###########################################################################
CODE 2
import java.util.Scanner; //importing to use scanner for taking input
public class Mathematics {
//private data members of class
private double x;
private double y;
//constructor
public Mathematics(double num1, double num2) {
x = num1; //assign value of num1 in x
y = num2; //assign value of num2 in y
}
//function to find sum
public void sum()
{
double result=x+y; //using + operator to find sum
System.out.println("Sum of "+ x +" and "+ y +" is: "+result);
}
//function to find subtraction
public void sub()
{
double result=x-y; //using - opereator to find subtraction
System.out.println("Subtraction of "+ x +" and "+ y +" is: "+result);
}
//function to find division
public void division()
{
double result=x/y; //using / operator to find division
System.out.println("Division of "+ x +" and "+ y +" is: "+result);
}
//main method
public static void main(String[] args) {
//variables to hold values
double x,y;
//creating object of Scanner to take input from user through System standard input
Scanner scan=new Scanner(System.in);
System.out.println("Enter value of x: ");
x=scan.nextDouble(); //taking double input using scanner nextDouble()
System.out.println("Enter value of x: ");
y=scan.nextDouble(); //taking double input using scanner nextDouble()
Mathematics obj=new Mathematics(x, y); //creating object of Mathematics class using x and y as parameters
obj.sum(); //calling sum()
obj.sub(); //calling sub()
obj.division(); //calling division()
scan.close(); //closing scanner
}
}
OUTPUT