100% found this document useful (1 vote)
85 views8 pages

Java Class Definitions for E-commerce and Education

The document contains specifications and Java code for six different classes: Eshop, Student, Bank, courier, emi_cal, and index_num. Each class includes member variables, methods for accepting input, calculating values, and displaying results based on specific criteria. The classes cover various scenarios such as online shopping, student allocation, banking calculations, courier charges, EMI calculations, and processing student index numbers.

Uploaded by

asha.bluebirds
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
85 views8 pages

Java Class Definitions for E-commerce and Education

The document contains specifications and Java code for six different classes: Eshop, Student, Bank, courier, emi_cal, and index_num. Each class includes member variables, methods for accepting input, calculating values, and displaying results based on specific criteria. The classes cover various scenarios such as online shopping, student allocation, banking calculations, courier charges, EMI calculations, and processing student index numbers.

Uploaded by

asha.bluebirds
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Question 1

Define a class called with the following specifications:


Class name: Eshop Member variables:
String name: name of the item purchased double price: Price of the item purchased Member methods:
void accept(): Accept the name and the price of the item using the methods of Scanner class.
void calculate(): To calculate the net amount to be paid by a customer, based on the following criteria:
Price Discount
1000 – 25000 5.0%
25001 – 57000 7.5 %
57001 – 100000 10.0%
More than 100000 15.0 %
void display(): To display the name of the item and the net amount to be paid. Write the main method to create an
object and call the above methods.
import [Link].*;
public class Eshop
{
String name;
double price;
void accept()
{
Scanner sc = new Scanner([Link]);
[Link]("Enter the name ");
name=[Link]();
[Link]("Enter price");
price=[Link]();
}
void calculate()
{
if(price>=1000 && price <=25000)
price=price-(price*0.05);
else if(price >=25001 && price<=57000)
price=price-(price*0.075);
else if (price >=57001 && price<=100000)
price=price-(price*0.10);
else
price=price-(price*0.15);
}
void display()
{
[Link]("name="+name);
[Link]("price="+price);
}
public static void main(String args[])
{
Eshop obj = new Eshop();
[Link]();
[Link]();
[Link]();
}
}
Question 2
Design a class with the following specifications:
Class name: Student
Member variables:
name — name of student
age — age of student
mks — marks obtained
stream — stream allocated
(Declare the variables using appropriate data types)
Member methods:
void accept() — Accept name, age and marks using methods of Scanner class.
void allocation() — Allocate the stream as per following criteria:
mks stream

>= 300 Science and Computer

>= 200 and < 300 Commerce and Computer

>= 75 and < 200 Arts and Animation

< 75 Try Again


void print() – Display student name, age, mks and stream allocated.
Call all the above methods in main method using an object.
import [Link];

public class Student


{
String name;
int age;
double mks;
String stream;

public void accept()


{
Scanner sc = new Scanner([Link]);
[Link]("Enter student name: ");
name = [Link]();
[Link]("Enter age: ");
age = [Link]();
[Link]("Enter marks: ");
mks = [Link]();
}

public void allocation()


{
if (mks < 75)
stream = "Try again";
else if (mks < 200)
stream = "Arts and Animation";
else if (mks < 300)
stream = "Commerce and Computer";
else
stream = "Science and Computer";
}
public void print()
{
[Link]("Name: " + name);
[Link]("Age: " + age);
[Link]("Marks: " + mks);
[Link]("Stream allocated: " + stream);
}

public static void main(String args[]) {


Student obj = new Student();
[Link]();
[Link]();
[Link]();
}
}
Question 3

Define a class with the following specifications:


Class name: Bank
Member variables:
double p — stores the principal amount
double n — stores the time period in years
double r — stores the rate of interest
double a — stores the amount
Member methods:
void accept () — input values for p and n using Scanner class methods only.
void calculate () — calculate the amount based on the following conditions:

Time in (Years) Rate %

Upto 1⁄2 9

> 1⁄2 to 1 year 10

> 1 to 3 years 11

> 3 years 12

n
a=p(1+r/100)
void display () — display the details in the given format.
Principal Time Rate Amount

XXX XXX XXX XXX

import [Link].*;
public class Bank
{
double p,n,r,a;
void accept()
{
Scanner sc = new Scanner([Link]);
[Link]("Enter p");
p=[Link]();
[Link]("Enter n");
n=[Link]();
}
void calculate()
{
if(n<=0.5)
r=9;
else if(n>0.5 && n<=1)
r=10;
else if(n>1 && n<=3)
r=11;
else
r=12;
a=p*[Link](1+(r/100.0),n);
}
void display()
{
[Link]("Principal"+"\t"+"Time"+"\t"+"Rate"+"\t"+"Amount");
[Link](p+"\t"+n+"\t"+r+"\t"+a);
}
public static void main(String args[])
{
Bank obj = new Bank();
[Link]();
[Link]();
[Link]();
}
}
Question 4 [15]
DTDC a courier company charges for the courier based on the weight of the parcel. Define a class with
the following specifications:
class name: courier
Member variables: name – name of the customer
weight – weight of the parcel in kilograms address – address of the recipient
bill – amount to be paid
type – ‘D’- domestic, ‘I’- international
Member methods:
void accept ( ) – to accept the details using the methods of the Scanner class only. void calculate ( ) –
to calculate the bill as per the following criteria:
Weight in Kgs Rate per Kg
First 5 Kgs Rs.800
Next 5 Kgs Rs.700
Above 10 Kgs Rs.500
An additional amount of Rs.1500 is charged if the type of the courier is I (International)
void print ( ) – To print the details
void main ( ) – to create an object of the class and invoke the methods

import [Link].*;
class courier
{
String name;
double weight;
String address;
double bill;
char type;
void accept()
{
Scanner sc = new Scanner([Link]);
[Link]("Enter customer's name: ");
name = [Link]();
[Link]("Enter recipient's address: ");
address = [Link]();
[Link]("Enter parcel weight (in Kgs): ");
weight = [Link]();
[Link]("Enter courier type");
[Link]("D (Domestic), I (International): ");
type = [Link]().charAt(0);
}

void calculate()
{
if (weight <= 5)
bill = weight * 800;
else if (weight>5 && weight<= 10)
bill = (5 * 800) + ((weight - 5) * 700);
else
bill = (5 * 800) + (5 * 700) + ((weight - 10) * 500);
if (type == 'I')
{
bill += 1500;
}
}

void print()
{
[Link]("Customer's Name: " + name);
[Link]("Parcel Weight: " + weight + " Kgs");
[Link]("Recipient's Address: " + address);
[Link]("Type of Courier: " + type);
[Link]("Bill Amount: Rs." + bill);
}

public static void main(String args[])


{
courier obj = new courier();
[Link]();
[Link]();
[Link]();
}
}
Question 5
1. The BHDB company offer EMI (Equated Monthly Instalments) based loans for the
purchase of electronic devices based on the purchase amount the rate of interest is
offered as follows:
Purchase amount less than Rs.20000, rate of interest is 12% otherwise the rate of
interest is 15%.
Amount with interest for the specified number of years is calculated using the formula
Amount = p(1+r/100)^n
Where p is the purchase amount, r is the rate of interest, n is the number of years.
After the amount is calculated it is converted into EMI by dividing the amount by the
number of months of the tenure, which has to be a whole number rounded off to the
nearest integer.
Print the details as follows:
Purchase amount:
Rate of interest:
Amount with interest:
EMI:
Define a class to accept the purchase amount and the number of years of the tenure,
calculate and print the details as per the above specifications. */
import [Link].*;
class emi_cal
{
public static void main(String args[])
{
double pa, r, amount; int n;double emi;
Scanner sc=new Scanner([Link]);

[Link]("enter purchase amount");


pa=[Link]();
[Link]("enter no. Of years");
n=[Link]();

if(pa<=20000)
r=12;
else
r=15;
amount=pa*[Link](1+r/100,n);
emi=amount/(n*12);

[Link]("purchase amount="+pa);
[Link]("rate of interest="+r);
[Link]("amount="+amount);
[Link]("emi="+[Link](emi));
}
}
Question 6
/* . A student appearing for the ICSE/ISC examination will be given an index number,
which is of the following format:
Number of 7 digits/number of 3 digits.
The first digit represents ICSE(1) or ISC(2), the next two digits represent the year, the
next four digits represent the centre number, the last 3 digits represent the index
number.
Example:
1244311/204
Class: 10
Year: 24
Centre number: 4311
Index number: 204
Example :
2259856/107
Class: 12
Year: 25
Centre number: 9856
Index number:107
Define a class to accept the student index number as a String and print his/her details
as above*/
import [Link].*;
class index_num
{
public static void main(String args[])
{
Scanner sc=new Scanner([Link]);
[Link]("enter n");
String s=[Link]();
int len=[Link]();
char c= [Link](0);
String year=[Link](1,3);
String centre=[Link](3, 7);
String index = [Link](len-3);
if(c=='1')
[Link]("Class : 10");
else if(c=='2')
[Link]. println("Class : 12");
[Link]("Year :"+year);
[Link]("Centre number :"+centre);
[Link]. println("Index number :"+index);
}
}

Common questions

Powered by AI

The logic for stream allocation in the Student class uses a series of conditional checks on the student's marks to match them with the correct stream. It first assigns "Try Again" for marks below 75, uses sequential ranges for "Arts and Animation" (75-199) and "Commerce and Computer" (200-299), and assigns "Science and Computer" for 300 and above. This pattern creates a multi-tiered allocation system that must comprehensively cover all potential inputs, ensuring no logical fall-through errors, which is critical for program correctness and robustness in a real-world educational system .

The Courier class calculates the bill based on the weight of the parcel using tiered pricing combined with a flat rate added for international shipping. For up to 5 kg, the rate is Rs. 800 per kg; for the next 5 kg (up to 10 kg), Rs. 700 per kg, and for any weight over 10 kg, Rs. 500 per kg. Additionally, a surcharge of Rs. 1500 is added if the courier type is international ('I'). This approach factors both the weight and the type of courier, ensuring a comprehensive billing system .

In the Index_num class, string manipulation is crucial to extract specific parts of the input index number that contain information about the student's class, exam year, centre number, and index number. String methods such as substring() are used to isolate these segments from the input string: the first character determines the class (ICSE or ISC), the next two characters are the year, followed by the centre number, and finally, the last three characters represent the student's index number. This methodical extraction process highlights the role of string manipulation in parsing and processing structured data formats .

The Eshop class uses a series of if-else-if control structures to determine the discount rate based on the price of an item. The price is checked against assigned ranges, with each range corresponding to a specific discount percentage: 5% for prices 1000-25000, 7.5% for 25001-57000, 10% for 57001-100000, and 15% for prices greater than 100000 .

The Student class allocates a stream based on the marks obtained by the student using multiple conditional statements. The class first checks if marks are less than 75 for which the stream allocated is "Try Again". For marks between 75 and 199, "Arts and Animation" is allocated. Marks between 200 and 299 are allocated "Commerce and Computer," and marks of 300 or more lead to "Science and Computer." This graduated check is necessary to correctly assign streams based on a hierarchical criterion of marks .

In the emi_cal class, the rate of interest is conditional based on the purchase amount. For amounts less than Rs. 20,000, the rate is 12%; otherwise, it is 15%. The amount with interest is calculated using the compound interest formula: Amount = p(1 + r/100)^n. After this, the total amount is divided by the total months in the loan term (n*12) to derive the EMI, which is rounded off to the nearest integer. This calculation ensures that the EMI adapts to both interest rate and tenure, providing a flexible financial solution .

The Bank class calculates the total interest amount by first determining the appropriate rate based on the time period (n) using conditional statements. It sets the rate (r) to 9% for up to 0.5 years, 10% for more than 0.5 to 1 year, 11% for 1 to 3 years, and 12% for more than 3 years. It then uses the formula for compound interest: a = p * (1 + r/100)^n to calculate the total amount, where p is the principal amount .

Instead of using Math.round(), which rounds to the nearest integer, an alternative rounding method for EMI could be to always round up using Math.ceil(). This guarantees that the borrower is consistently prepared for the maximum potential monthly payment, ensuring financial prudence. This method could help reduce the risk of underestimating EMIs due to fractional values, especially in long tenures where slight underestimations could accumulate significantly. Moreover, adopting Math.ceil() might lead to the structuring of payments in a way that slightly accelerates principal repayment, potentially reducing interest accumulation over time .

The Courier class uses nested conditions to determine parcel costs and additional charges for international parcels. While this provides a clear logical flow for anyone modifying the base rates or adding new conditions, it introduces complexity that might hinder maintainability, as future changes could necessitate multiple condition adjustments. Moreover, extensive nesting can impact performance as the decision tree grows, especially if conditions need to be re-evaluated frequently or if the input space increases. Thus, careful documentation and possibly restructuring using a more scalable logic structure like switch-case could mitigate these issues .

The Bank class uses conditional statements to select the appropriate interest rate based on time. While effective for simple range checks, this approach can become cumbersome as more conditions are introduced. An improvement could be to employ a Map structure to store ranges and their corresponding rates, allowing for more dynamic updates and constant-time lookups. This change would simplify rate management, improve readability, and potentially enable users to define new tiers without altering the core logic, thereby enhancing both flexibility and maintainability .

You might also like