0% found this document useful (0 votes)
68 views6 pages

21BCE7761 Narendra CSE Assignment-3

The document describes an assignment to create classes in Java to represent bank accounts and books. It includes: 1. A Bank class with data members for account name, number, and balance, and methods to deposit, withdraw, and display account details. 2. An abstract Book class with title and price fields and get methods, and subclasses Fiction and NonFiction that set prices at $24.99 and $37.99 respectively. 3. A CollegeCourse class with department, number, credits, and fee, and a LabCourse subclass that adds $50 to fees.

Uploaded by

NARENDRA
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
0% found this document useful (0 votes)
68 views6 pages

21BCE7761 Narendra CSE Assignment-3

The document describes an assignment to create classes in Java to represent bank accounts and books. It includes: 1. A Bank class with data members for account name, number, and balance, and methods to deposit, withdraw, and display account details. 2. An abstract Book class with title and price fields and get methods, and subclasses Fiction and NonFiction that set prices at $24.99 and $37.99 respectively. 3. A CollegeCourse class with department, number, credits, and fee, and a LabCourse subclass that adds $50 to fees.

Uploaded by

NARENDRA
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
You are on page 1/ 6

CSE Assignment – 3

K.Narendranath Reddy (21bce7761)


1. Design a class in java to represent a Bank that includes the following details.
Implement the below scenario using Class and objects in Java.

a. Data Members

i. Account Name

ii. Account Number

iii. Balance amount in the account

b. Member Functions

i. Assign initial values

ii. Deposit Rs.5000 into an account

iii. Withdraw an amount Rs.2000 after checking bank balance

iv. Display the Account holder name & Balance

Code:
import java.util.Scanner;
import java.util.concurrent.Flow.Subscriber;

class bank{
String name;
int account_number;
int balanc_amount;
int deposit;
int withdrawl;
Scanner sin=new Scanner(System.in);
bank(String n ,int account,int balanc,int depost,int withdraw){
name=n;
account_number=account;
balanc_amount=balanc;
deposit=depost;
withdrawl=withdraw;
}
void getdetails(){
System.out.println(" ----------------------------------------- ");
System.out.println("name - "+name);
System.out.println("account number -"+account_number);
System.out.println("balance amount before transaction-
"+balanc_amount);
System.out.println("with drawn amount -"+withdrawl);
balanc_amount=balanc_amount+deposit-withdrawl;
System.out.println("balance amount after transaction -
"+balanc_amount);
System.out.println(" ----------------------------------------- ");
}
public static void main(String[] args) {
Scanner sin=new Scanner(System.in);
System.out.println("ENTRE THE NAME -");
String name=sin.next();
System.out.println("enter the account number ");
int account_number=sin.nextInt();
System.out.println("enter the balance amount -");
int balanc_amount=sin.nextInt();
System.out.println("enter the desposit amount -");
int deposit=sin.nextInt();
System.out.println("enter the with drawl amount ->");
int withdrawl=sin.nextInt();
if(balanc_amount<withdrawl){
System.out.println("not possible ");}
else{
bank p=new bank(name, account_number, balanc_amount, deposit,
withdrawl);
p.getdetails();}
}}

Output :
4. Create an abstract class named Book. Include a String field for the book’s title
and a double field for the book’s price. Within the class, include a constructor that
requires the book title, and add two get methods—one that returns the title and
one that returns the price. Include an abstract method named setPrice(). Create
two child classes of Book: Fiction and NonFiction. Each must include a setPrice()
method that sets the price for all Fiction Books to $24.99 and for all NonFiction
Books to $37.99. Write a constructor for each subclass, and include a call to
setPrice() within each. Write an application demonstrating that you can create
both a Fiction and a NonFiction Book, and display their fields.
import java.util.*;
abstract class Book
{
String tit;
double pric;
Book()
{
tit="";
pric=0.0;
}
Book(String btit)
{
tit=btit;
pric=0.0;
}
String gettitle()
{
return tit;
}
double getprice()
{
return pric;
}
void setPrice()
{
}
}
class Fiction extends Book
{
Fiction(String btitle)
{
super(btitle);
setPrice();
}
void setPrice()
{
super.pric=24.99;
}
}
class NonFiction extends Book
{
NonFiction(String btitle)
{
super(btitle);
setPrice();
}
void setPrice()
{
super.pric=37.99;
}
}
public class java
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a fictional book: ");
String fb=sc.nextLine();
Fiction ob=new Fiction(fb);
ob.setPrice();
System.out.println("Enter a Non-fictional book: ");
String nfb=sc.nextLine();
NonFiction obj=new NonFiction(nfb);
obj.setPrice();
System.out.println("Book Title: "+ ob.tit);
System.out.println("Genre: Fiction");
System.out.println("Price: "+ ob.pric);
System.out.println("Book Title: "+ obj.tit);
System.out.println("Genre: Non-Fiction");
System.out.println("Price: "+ obj.pric);

}}
output:
3. Create a class named CollegeCourse that includes data fields that hold the
department (for example, ENG), the course number (for example, 101), the
credits (for example, 3), and the fee for the course (for example, $360). All of the
fields are required as arguments to the constructor, except for the fee, which is
calculated at $120 per credit hour. Include a display() method that displays the
course data. Create a subclass named LabCourse that adds $50 to the course fee.
import java.util.*;
class CollegeCourse
{
String dep;
int cno, cred;
double fee;
CollegeCourse()
{
dep="";
cno=cred=0;
fee=0.0;
}
CollegeCourse(String dept,int crno, int crd)
{
dep=dept;
cno=crno;
cred=crd;
fee= 120*crd;
}
void disp()
{
System.out.println("Course Name: ");
System.out.println(dep+cno);
System.out.println("Credits "+ cred);
System.out.println("Fee: "+ fee);
}
}
class LabCourse extends CollegeCourse
{

LabCourse(String dept,int crno, int crd)


{
super(dept,crno,crd);
fee=super.fee+50;
}
void disp()
{
System.out.println("Course Name: ");
System.out.println(dep+cno);
System.out.println("Credits "+ cred);
System.out.println("Fee: "+ fee);
}
}
public class java
{
public static void main(String[ ] args)
{
Scanner sc= new Scanner(System.in);
System.out.println("Enter the department name in UPPERCASE: ");
String dpt=sc.next();
System.out.println("Enter the course number: ");
int crno=sc.nextInt();
System.out.println("Enter the credits: ");
int crd=sc.nextInt();
CollegeCourse ob= new CollegeCourse(dpt, crno,crd);
if(dpt.equals("BIO") ||dpt.equals("CHY") || dpt.equals("CIS") ||
dpt.equals("PHY") )
{
LabCourse obj= new LabCourse(dpt, crno,crd);
obj.disp();
}
else
{
ob.disp();
}
}
}
Output:

You might also like