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
Tanner has had many different jobs. He previously designed the playscape at the local park with natural rocks, creeks, and veget
zavuch27 [327]
I think it would be Design. 
3 0
3 years ago
Read 2 more answers
Which type of document would be best created in Word? HELLP!!!!
bekas [8.4K]
I think it’s presentation.
Like if I am right.
8 0
3 years ago
Read 2 more answers
What is the output of the following code snippet? int age = 25; if (age > 30) { System.out.println("You are wise!"); } else {
Lunna [17]

Answer:

You have much to learn!

Explanation:

In the question the variable age has been declared and assigned the value of 25.

The if Statement checks if age >30 to output "You are wise!" else it will output "You have too much to learn"

Since age has been assigned the value of 25, we expect the program to execute the else block which is "You have too much to learn"

4 0
3 years ago
Jarvis wants to buy a basic scanner for daily use. Which scanner will be suitable for him?
Romashka-Z-Leto [24]

Answer:

A

Flatbed scanner often come with sheet feeders for scanning multiple sheets of paper rather than one at time

8 0
3 years ago
Read 2 more answers
Explain test-driven development and provide an example of how it can be used in your
dalvyx [7]

Answer:

 The test- driven development is basically use in the programming language for the designing various type of test cases in the computer system. The main goal of the test driven development is that it uses to make the system bug free and simple.

It basically use in the organization to avoid various duplication in the code and make the function more efficient.

The basic approach of the test driven development is that the test are develop so that it specify the function for the code to perform.

8 0
4 years ago
Other questions:
  • What shooting position is commonly used when hunting with a shotgun?
    14·2 answers
  • Write a program that converts temperatures in the Celsius scale to temperatures in the Fahrenheit scale. Your program should ask
    14·1 answer
  • What command prompts should be used to assign an IP address to:
    9·1 answer
  • Which of the following is a software application program?
    12·1 answer
  • Look at the trend in the US labor force participation rate. If increasing the number of women and men in the labor force can be
    13·1 answer
  • Which print setting enables multiple slides to be printed on one page?
    5·2 answers
  • What will happen when you drag and drop a worksheet tab into another workbook WITHOUT holding the Ctrl key down?
    13·2 answers
  • Create a high-level plan to perform a gap analysis for Fullsoft Inc
    7·1 answer
  • How would the grain crusher work
    11·2 answers
  • Which option best describes top-down design?
    10·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!