java program
write a class to input a name of a person and age in two separate variables using scanner class. convert age in class (1 year=365 days) and print the same.
WARNING- IF U DONT KNOW THE ANSWER, DONT REPLY. FOMY SPAM. DONT REPORT. PLEASE
Answers
Answer:
We make computer programs for users. We solve real-world computer problems for users. So, it is necessary for our computer to interact with the users. This is what you will learn in this chapter.
We are done with operators and know how to print something on the screen. In this section, we will learn about how to take input from a user. So, yeah now you can take input from a user and display something on the screen. Let's proceed.
In Java, we input with the help of the Scanner class. Java has a number of predefined classes which we can use. We will learn more about classes later.
Predefined classes are organized in the form of packages. This Scanner class is found in java.util package. So to use the Scanner class, we first need to include java.util package in our program.
We include a package in a program with the help of import keyword. We can either import the java.util.Scanner class or the entire java.util package.
To import a class or a package, add one of the following lines to the very beginning of your code.
import java.util.Scanner; // This will import just the Scanner class
import java.util.*; // This will import the entire java.util package
After importing, we need to write the following statement in our program.
Scanner s = new Scanner (System.in);
Here by writing Scanner s, we are declaring s as an object of Scanner class. System.in within the round brackets tells Java that this will be System Input i.e. input will be given to the system.
If you have understood all that has been stated till now, it's a good thing. If you haven't, do not bother. Currently, you just need to know that we have to import the class or package at the beginning of the program and then include the above line in the program.
How to Input
Taking a value from a user is quite easy. You just have to write a statement and it's done!
Let's start with integer. Consider the following code
Explanation:
We make computer programs for users. We solve real-world computer problems for users. So, it is necessary for our computer to interact with the users. This is what you will learn in this chapter.
We are done with operators and know how to print something on the screen. In this section, we will learn about how to take input from a user. So, yeah now you can take input from a user and display something on the screen. Let's proceed.
In Java, we input with the help of the Scanner class. Java has a number of predefined classes which we can use. We will learn more about classes later.
Predefined classes are organized in the form of packages. This Scanner class is found in java.util package. So to use the Scanner class, we first need to include java.util package in our program.
We include a package in a program with the help of import keyword. We can either import the java.util.Scanner class or the entire java.util package.
To import a class or a package, add one of the following lines to the very beginning of your code.
import java.util.Scanner; // This will import just the Scanner class
import java.util.*; // This will import the entire java.util package
After importing, we need to write the following statement in our program.
Scanner s = new Scanner (System.in);
Here by writing Scanner s, we are declaring s as an object of Scanner class. System.in within the round brackets tells Java that this will be System Input i.e. input will be given to the system.
If you have understood all that has been stated till now, it's a good thing. If you haven't, do not bother. Currently, you just need to know that we have to import the class or package at the beginning of the program and then include the above line in the program.
How to Input
Taking a value from a user is quite easy. You just have to write a statement and it's done!
Let's start with integer. Consider the following code
Answer:
Java – Calculate age from date of birth
To calculate age from date of birth for any person seems a really simple thing to do and it is indeed. In a very broad sense, I can visualize three solutions for this age calculator program
Explanation:
Java 8 Period class
Java 8 program to calculate age of a person from date of birth. It uses Period class to store the differences between two LocalDate instances. The two dates are todays date and date of birth.
Once Period class is created, we can get the difference between both dates in desired metrics.