Answer to Question #264364 in C++ for 1|-||2|=

Question #264364

(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.


1
Expert's answer
2021-11-13T23:51:22-0500
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);
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS