Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
44 views

List of Assignments Java

The document discusses classes and objects in Java. It provides examples of: 1. Creating an AccountHolder class with instance variables like account number, name, balance and methods like constructors, getters/setters, deposit, withdraw. An array of AccountHolder objects is created and a menu driven program is implemented to add, display, deposit and withdraw from accounts. 2. Creating a Student class with roll number, name, percentage and implementing necessary methods. Static variable is used to print number of Student objects created. 3. Implementing getter/setter methods and toString method in the Student class. 4. Creating Student and Batch classes in different packages and creating objects to demonstrate packages.

Uploaded by

Atharv Halmadge
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views

List of Assignments Java

The document discusses classes and objects in Java. It provides examples of: 1. Creating an AccountHolder class with instance variables like account number, name, balance and methods like constructors, getters/setters, deposit, withdraw. An array of AccountHolder objects is created and a menu driven program is implemented to add, display, deposit and withdraw from accounts. 2. Creating a Student class with roll number, name, percentage and implementing necessary methods. Static variable is used to print number of Student objects created. 3. Implementing getter/setter methods and toString method in the Student class. 4. Creating Student and Batch classes in different packages and creating objects to demonstrate packages.

Uploaded by

Atharv Halmadge
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Classes and Objects

1. Create a class AccountHolder with -


Instance variables - account number, account holder name, account balance
Class methods - constructors, getter/setter methods, deposite, withdraw
Create an array of AccountHolder objects in main.
Write a menu driven program to perform -
1. Add record for account holder
2. Display details of all account holders.
3. Deposite an amount into perticular account
4. Withdraw an amount from perticular account

Code:

package com.atharv;
import java.util.ArrayList;
import java.util.Scanner;

public class AccountHolder {

private int account_number;


private String name;
private double acc_balance;

public AccountHolder()
{

public AccountHolder(int num, String n, double b)


{
account_number = num;
name = n;
acc_balance = b;
}

public int getAccount_number() {


return account_number;
}
public void setAccount_number(int account_number) {
this.account_number = account_number;
}

public String getName() {


return name;
}

public void setName(String name) {


this.name = name;
}

public double getAcc_balance() {


return acc_balance;
}

public void setAcc_balance(double acc_balance) {


this.acc_balance = acc_balance;
}

public void CreateAccount()


{
Scanner scan = new Scanner(System.in);
System.out.println("Enter Account Number: ");
account_number = scan.nextInt();
System.out.println("Enter Account holder Name: ");
name = scan.next();
System.out.println("Enter Account Balance: ");
acc_balance = scan.nextDouble();

public void deposite(double amount)


{
acc_balance = acc_balance + amount;
System.out.println(amount+" Deposited successfully");
}

public void withdraw(double amount)


{
if (acc_balance <= 0 || acc_balance < amount)
{
System.out.println("Insufficient Balance");
}
else
{
acc_balance = acc_balance + amount;
System.out.println(amount+" Withdraw successfully");
}
}

public void display()


{
System.out.println("Account Number: "+account_number+" Account Holder Name:
"+name+" acc_balance: "+acc_balance);
}

public static void main(String[] args) {


ArrayList<AccountHolder> list = new ArrayList<>();
while(true)
{
System.out.println("Select options to perform Following actions");
System.out.println("1. Add account");
System.out.println("2. Display account details");
System.out.println("3. Enter deposite amount");
System.out.println("4. Enter withdraw amount");
System.out.println("5. Exit");
Scanner scan = new Scanner(System.in);
int choise = scan.nextInt();

switch(choise)
{
case 1:
AccountHolder ac = new AccountHolder();
ac.CreateAccount();
list.add(ac);
break;
case 2:
for(AccountHolder acc:list)
{
acc.display();
}
break;
case 3:
System.out.println("Enter account number for which you want to deposite
amount: ");
int accountNumberDeposite = scan.nextInt();
System.out.println("Enter amount to deposite");
double amount = scan.nextDouble();
for(AccountHolder ah:list)
{
if(ah.getAccount_number()==accountNumberDeposite)
{
ah.deposite(amount);
}

}
break;
case 4:
System.out.println("Enter account number for which you want to Withdraw
amount: ");
int accountNumberwithdraw = scan.nextInt();
System.out.println("Enter amount to withdraw");
double amounttowithdraw = scan.nextDouble();
for(AccountHolder ah:list)
{
if(ah.getAccount_number()==accountNumberwithdraw)
{
ah.deposite(amounttowithdraw);
}

}
break;
case 5:
System.exit(0);

}
}

}
}

2. Write a class Student with members for rollno, name and percentage. Implement necessary
methods inside class. Write a code to print number of objects created for class Student. Use
static.
Code:

package com.atharv;

public class student {

private int rollno;

private String name;

private double percentage;

static int count;

public student()

count++;

public student(int r, String n, double per)

rollno = r;

name = n;

percentage = per;

count ++;

public static void main(String args[])

student s1 = new student(101, "A", 89);

student s2 = new student(102, "C", 79);


student s3 = new student(103, "B", 69);

student s4 = new student();

System.out.println(student.count);

3. Implement getter/setter methods and method “toString” in above class Student.


Code:

Packages

4. Write a class Student in package com.kpit.pack1


members - RollNo and Name.
Write a class Batch in package com.kpit.pack2
members - CourseName and BatchStrength.
Implement constructors with display method in each class.
Create objects of above classes in main method which is written in default package.

Containment, Inheritance & Polymorphism

5. Write a class Student having following –


a. Student Roll Number (int)
b. Student Name (String)
c. Date of Birth (Date class object where Date is user defined class)
Implement default constructor, parameterized constructor, accept, display. Generate the
student roll number automatically.

6. Construct a hierarchy of employees.


a. Create an Employee class with attributes like employee id, name, date of birth.
b. Inherit class WageEmployee from super class Employee
c. WageEmployee class should have following members
a. Number of hours worked
b. Rate per hour
d. Inherit class SalesPerson from super class WageEmployee. SalesPerson should have following
members
a. Number of items sold
b. Commission per item
e. Write constructors for WageEmployee and SalesPerson classes.
f. Override the methods for displaying details, calculating salary in WageEmployee and
SalesPerson class.
WageEmployee Salary = hours * rate
SalesPerson Salary = hours*rate + sales*commission

7. Construct a hierarchy of employees.


1. Create an Employee class with attributes like employee id, name, basic salary.
2. Inherit class Manager and MarketingExecutive from super class Employee
3. Manager class should have following members
a. Petrol Allowance: 8% of basic salary
b. Food Allowance: 12% of basic salary
c. Other Allowance: 4% of basic salary
4. MarketingExecutive class should have following members
a. Kilometers travelled
b. Tour Allowance: Rs.5/- per kilometer
c. Telephone Allowance Rs.2000/-
5. Write constructors for the derived classes. (Use super keyword)
6. Implement methods - display, calculateGrossSalary and calculateNetSalary in Manager
and MarketingExecutive class.
gross salary = basic salary + allowances
net salary = gross salary - PF
PF = 12.5% of basic salary
7. Declare method printObjects in client code as -
void printObjects(Employee e)
{
//----
}
Pass objects of different classes to printObjects method and display corresponding details.

8. Declare an interface printable as given below.


interface printable
{
public void printDetails();
}
Write a class CktPlayer with members - name and runs
Write a class FtPlayer with members - name and goals
CktPlayer and FtPlayer implements printable interface and overrides its method to
display details.

9. Take attributes for number, name and price in class Vehicle. Implement “equals” and
“hashcode” methods for class Vehicle.

10. Clone the objects of above class Vehicle by implementing “cloneable” interface.

Collections and Generics

11. Write a class Employee for an application which will have data members for employee id,
employee name and salary. Provide the following functionalities in Employee class.
1. Initialing objects using default and parameterized constructors.
2. Accepting and displaying the information of employee from console
In "main" method take an array list of Employee objects. Write a menu driven program to -
a. Insert record into an array list.
b. Update information of specific employee on the basis of emp_id accepted from user
c. Display all records.

12. a. Write a student class with attributes -


class Student
{
private int rollno;
private String name;
private double percentage;
private Set<String> skillset

// methods of Student class


}

b. Now write a class 'UtilityList' which has list of Student objects as member.
class UtilityList
{
private List<Student> list;
// methods of Utility class
}

Implement methods 'createList()' and 'printList()' in class UtilityList.


createList - will accept Students from user and will store it into list of students.
printList - will print whole list of students.

c. Write a class 'UtilityReport' has a method 'showReport()' This method shows report like:
StudentName-->Percentage
class UtilityReport
{
private Map<String, Double> m;
// method of class UtilityReport
}

13. Sort the objects of class Student (implemented earlier) according to their percentage. Use
comparator interface
14. Sort the objects of class Student (implemented earlier) according to their percentage. Use
comparable interface

Exception Handling

15. Create a class Account with -

Instance variable - balance


Class methods - deposite/withdraw
User withdrawal limit on one transaction is Rs.15000/-
Throw and Handle Exceptions -
1. OverLimit - when user tries to withdraw more than Rs. 15000/- in a transaction
2. InsufficientBalance - When user withdrawal amount is more than existing balance

Multithreading

16. Create two different threads t1 and t2.


t1 ==> prints next 10 incremented values of user entered number
t2 ==> prints multiplication table of the user entered number

17. Refer class Account implemented in question 19. Make its methods synchronized to avoid
thread interference.
18. Display a filled circle in a frame. Make this circle to move/toggle between left and right walls of
frame. Use multithreading.

19. There are three circles in a frame (red, blue green). These circles moves from left to right with
constant speed. Red is fastest moving. Green is slowest moving and blue is moving with
intermediate speed. All of them start at same time. Whichever reaches to right edge will wait for
others to come. When last one(green) will reach to right edge, all of them will again start the
execution from left.

JDBC

20. Create table student in database with fields for rollno, name and percentage.
Using JDBC perform following operations on this table through menu driven java program.
a. Insert record
b. Update record
c. Delete record
d. Display particular record from table
e. Display all records from table

File Handling

21. Serialize and de-serialize objects of class Student into a file student.txt

You might also like