(Pointers to objects)
Define a class 'Item' that is used to store and display the information regarding the item
number and its price. Write a program to store and display the details of one
items by using both normal object and pointer to object separately. Display
appropriate message wherever necessary.
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Item t=new Item();
t.getData();
t.display();
Item r=new Item();
r.getData();
r.display();
}
}
class Item{
private int itemNumber;
private int itemPrice;
public void getData(){
Scanner in =new Scanner(System.in);
System.out.println("Enter item number: ");
itemNumber=in.nextInt();
System.out.println("Enter item price: ");
itemPrice=in.nextInt();
}
public void display(){
System.out.println("Item number = "+itemNumber);
System.out.println("Item price = "+itemPrice);
}
}
Comments
Leave a comment