1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
otez555 [7]
3 years ago
11

write a method that will return a person's age entered by keyboard. You will need to include at least one instance variable in t

his program. No parameters are necessary for this question. You must NOT print out the age from within the return method but rather from another method. in java =D
Computers and Technology
1 answer:
slamgirl [31]3 years ago
4 0

Answer:

This JAVA program shows the implementation of instance variable and methods.

import java.util.Scanner;

import java.lang.*;

public class Person {    

   Scanner sc = new Scanner(System.in);    

   // instance variable declared without static keyword and outside of all methods

   int age;    

   void getAge()

   {

       System.out.println("Enter your age");

       age = sc.nextInt();

   }    

   void display()

   {

       System.out.println("Entered age is " + age);

   }    

   public static void main(String args[]) {        

       Person ob = new Person();        

       ob.getAge();

       ob.display();

   }

}

OUTPUT

Enter your age

45

Entered age is 45

Explanation:

A class contains its own variables and methods. An instance variable is declared inside the class and outside of all the methods contained in that class.

As specified in the question, the class Person contains one instance variable age and two methods, along with main() method.

Along with the instance variable age, an object of the Scanner class is created to enable input from the user. Both these variables are outside the methods. To enable user input, the required java package has been imported.

The two methods are defined to receive age and display age as mentioned in the question.

The input age is stored in the instance variable age.

void getAge()

   {

       System.out.println("Enter your age");

       age = sc.nextInt();

   }

This variable is used in display() to print the age.

void display()

   {

       System.out.println("Entered age is " + age);

   }

   

All the methods are contained in a single class. Despite this, other methods can only be called through the object of the class which shows that JAVA is purely object oriented language.

public static void main(String args[]) {

       

       Person ob = new Person();

       

       ob.getAge();

       ob.display();

   }

As seen, the instance variable can be used by all methods contained in that particular class. The object of the class is only needed to call the method and not needed to use the instance variables.

You might be interested in
You have two microservices that need to communicate with each other without holding up a thread on either end. One service will
Yakvenalex [24]

Answer: E. Use a RESTful architecture for both, send the ID through a POST, and ping the service with a GET until a response is available.

Explanation:

Since there are two microservices that need to communicate with each other without holding up a thread on either end, the communication framework should be used is a RESTful architecture for both, send the ID through a POST, and ping the service with a GET until a response is available.

REST is a software architectural style which is used in defining the set of rules that be used for the creation of web services. The REST architecture allows requesting systems to be able to access and manipulate web resources.

8 0
3 years ago
What is HDLC flow control?​
Likurg_2 [28]
HDLC is a synchronous Data Link layer bit-oriented protocol developed by the International Organization for Standardization (ISO).
6 0
3 years ago
Which formulas would work to combine cells with first, middle, and last names from columns A through C and row 2 into a new cell
AURORKA [14]

Split? not really sure but i understand a bit of this..

6 0
3 years ago
Read 2 more answers
Computer that process digital as well as analogue signals​
Nuetrik [128]

Answer:

The answer is hybrid computer coz it is the combination of both analog and digital computers

Explanation:

hope it helps

good day

5 0
3 years ago
What game is this? help meee
olya-2409 [2.1K]

Answer:

I am guessing animal crossing

4 0
3 years ago
Read 2 more answers
Other questions:
  • Food is shipped thousands of miles throughout our country using various types of transportation such as trucks, planes, and boat
    10·1 answer
  • Lesson 15: Integrated Literacy Connections Education Introduction to Computer Applications Unit 6: Internet Safety
    5·1 answer
  • 17) you need to locate an article that (1) is published by a university or proffessional association(2) is authored by clearly d
    6·1 answer
  • To expand a window to the full size of the screen.​
    6·2 answers
  • Describe the difference between a chained conditional and a nested conditional. Give your own example of each.
    5·1 answer
  • Course Aggregation Many times, departments are only interested with how students have done in courses relative to a specific maj
    14·1 answer
  • Create an java application that will process 3-digit numbers as follows:
    8·1 answer
  • True or false scientists investigate and seek to explain the natural world
    14·1 answer
  • When constructing policies regarding data _______________, it is important that these policies offer particular guidance on sepa
    14·1 answer
  • suppose a malloc implementation returns 8-byte aligned addresses and uses an explicit free list where the next and previous poin
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!