21BCE7761 Narendra CSE Assignment-3
21BCE7761 Narendra CSE Assignment-3
a. Data Members
i. Account Name
b. Member Functions
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
{