C Programs Tutorials | IT Developer
IT Developer

Java Programs



Share with a Friend

Class & Object Based Program - CloudStorage - Java Programs

Define a class named CloudStorage with the following specifications:


Member Variables:
int acno – stores the user’s account number
int space – stores the amount of storage space in GB purchased by the user
double bill – stores the total price to be paid by the user


Member Methods:
void accept() – prompts the user to input their account number and storage space using Scanner class methods only.
void calculate() – calculates the bill total price based on the storage space purchased using the pricing table provided:

 

Storage range

Price per GB (Rs)

First 15 GB

15

Next 15 GB

13

Above 30 GB

11

 

void display() – displays the account number, storage space and bill to be paid.

Write a main method to create an object of the class and invoke the methods of the class with respect to the object.

 

import java.util.Scanner; class CloudStorage{ int acno; int space; double bill; public void accept(){ Scanner in = new Scanner(System.in); System.out.print("Account number: "); acno = Integer.parseInt(in.nextLine()); System.out.print("Space: "); space = Integer.parseInt(in.nextLine()); } public void calculate(){ if(space <= 15) bill = space * 15; else if(space <= 30) bill = 225 + (space - 15) * 13; else bill = 420 + (space - 30) * 11; } public void display(){ System.out.println("Account No. " + acno); System.out.println("Storage space: " + space); System.out.println("Bill: " + bill); } public static void main(String[] args){ CloudStorage obj = new CloudStorage(); obj.accept(); obj.calculate(); obj.display(); } }

Output

 
OUTPUT 1:
Account number: 1001
Space: 12
Account No. 1001
Storage space: 12
Bill: 180.0

OUTPUT 2:
Account number: 1002
Space: 25
Account No. 1002
Storage space: 25
Bill: 355.0