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]
2 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]2 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
The data I collect in Google Forms are all compiled in a spreadsheet for me.<br> False <br> True
Mandarinka [93]
I think it's false. If I'm wrong then sorry.
5 0
3 years ago
Read 2 more answers
At regular intervals the AP in an infrastructure network or wireless device in an ad hoc network sends a ____ frame both to anno
pshichka [43]

Answer:

beacon

Explanation:

At regular intervals the AP in an infrastructure network or wireless device in an adhoc network sends a beacon frame both to announce its presence and to provide the necessary information for other devices to join the network.

5 0
3 years ago
Software applications called _____ provide the means to record information that passes through a computer or router that is hand
Bezzdna [24]

Answer:

sniffer programs

Explanation:

Software applications called <u>sniffer programs</u>  provide the means to record information that passes through a computer or router that is handling Internet traffic.

3 0
3 years ago
In mathematics, "quadrant I" of the cartesian plane is the part of the plane where x and y are both positive. Given a variable,
lbvjy [14]

Answer:

#include <iostream>

using namespace std;

struct Cartesian {

double x;

double y;

};

int main() {

// creating a pointer p of type struct Cartesian

struct Cartesian *p = new Cartesian ;

cout << " Enter x: ";

// accessing the structure variable x by arrow "->"

cin >> p->x;

cout << "Enter y: ";

// accessing the structure variable y by arrow "->"

cin >> p->y;

// expression to check whether x and y lie in Quadrant 1

if (p->x > 0 && p->y > 0) {

 cout << "X and Y are in Quadrant 1 ";

}

else

{

 cout << "x And y are not in Quadrant 1";

}

// deleting the struct pointer p

delete p;

return 0;

}

Explanation:

in order to locate memory in heap, keyword "new" is used in C++ so,

struct Cartesian *p = new Cartesian ;

in order to access data members of the structure using pointer we always use an arrow "->". otherwise a dot oprerator is used to access data members.

in order to check whether x and y lie in 1st quadrent, we must use && operator in our if condition. so

if (p->x > 0 && p->y > 0)

&& will return true iff x and y both are +ve.

deleting the struct pointer p is important beacuse we are allocating memory in heap so heap memory should be used a resource and must be release when not needed otherwise it can cause memory leakage problems. so  

delete p;

5 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
2 years ago
Other questions:
  • Which is true for a hosted blog software
    15·2 answers
  • When looking to ensure your website is easily accessible by mobile users, what should you focus on doing first
    8·1 answer
  • You can add envelopes to existing documents. <br> a. True <br> b. False
    10·1 answer
  • Which of the following is a correct definition of the term rectification? A. Rectification is the opposition to current flow in
    14·2 answers
  • Which is an internet service?<br><br> Antivirus<br> Chat<br> Firewall<br> Router
    8·2 answers
  • What is Java Script?
    13·1 answer
  • The Spanning Tree Protocol operates at the Network layer of the OSI model.
    13·1 answer
  • You want to deploy a software package that's available to all users in the domain if they want to use it, but you don't want the
    7·1 answer
  • What is the code for forgot password on messenger​
    9·1 answer
  • Edhesive 2.3 code practice question 1​
    11·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!