0% found this document useful (0 votes)
17 views14 pages

Neelu

Uploaded by

ktmking1996
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)
17 views14 pages

Neelu

Uploaded by

ktmking1996
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/ 14

EXERCISE ­ 2

1.Arrays.java

import java.u l.*;


public class Array {
public sta c double getaverage(ArrayList<Integer> arr) {
double sum = 0;
for (int i = 0; i < arr.size(); i++) {
sum += arr.get(i);
}
return sum / (double) arr.size();
}
public sta c int getSum(ArrayList<Integer> arr) {
int sum = 0;
for (int i = 0; i < arr.size(); i++) {
sum += arr.get(i);
}
return sum;
}
public sta c int getSmall(ArrayList<Integer> arr) {
int min = Integer.MAX_VALUE;
for (int i = 0; i < arr.size(); i++) {
if(arr.get(i)<min){
min=arr.get(i);
}
}
return min;
}
public sta c int getGreat(ArrayList<Integer> arr) {
int max = Integer.MIN_VALUE;
for (int i = 0; i < arr.size(); i++) {
max = Math.max(max, arr.get(i));
}
return max;
}
public sta c void main(String[] args) {
Scanner sc = new Scanner(System.in);

System.out.println("Enter the upper bound : ");

ArrayList<Integer> arr=new ArrayList<>();


while (true) {
System.out.println("Enter the the number:");
int num = sc.nextInt();
if (num == -1) {
break;
}
arr.add(num);
double average = getaverage(arr);
int sum = getSum(arr);
int smallest = getSmall(arr);
int greatest = getGreat(arr);
System.out.println("Sum of the elements are: " + sum);
System.out.println("Average of the elements are: " + average);
System.out.println("Smallest of the elements are: " + smallest);
System.out.println("Greatest of the elements are: " + greatest);
}
sc.close();
}
}

2.Account.java

i)
public class Account {
int acc_no;
int balance;
String name;
public Account(int ac_no,int balance,String name){
this.acc_no=ac_no;
this.balance=balance;
this.name=name;
}
void deposit(int amount){
this.balance+=amount;
}
void withDraw(int amount){
if(this.balance<amount){
System.out.println("Can't withdraw the amount, Insufficient balance");
}
else{
System.out.println("amout withdraw successfull");
this.balance-=amount;
}
}
String getName(){
return this.name;
}
int getAccno(){
return this.acc_no;
}
int getBalance(){
return this.balance;
}
}

2. ii) AccountTest.java

public class AccountTest {


public sta c void main(String[] args) {
Account ac=new Account(1000,1000,"Neelakandan");
System.out.println("The name of the customer: "+ac.getName());
System.out.println("The balance amount of the customer: "+ac.getBalance());
System.out.println("The account no. : 0"+ac.getAccno());
//ac.deposit(1000);
ac.withDraw(300);
ac.withDraw(500);
ac.withDraw(700);
}
}

3. i) Customer.java

public class Customer {


int acc_no;
int balance_begin;
int new_balance;
int tot_charge;
int credit;
String name;
public Customer(int ac_no,int balance,String name){
this.name=name;
this.acc_no=ac_no;
this.balance_begin=balance;
}
int getAccno(){
return this.acc_no;
}
int getBeginBalance(){
return this.balance_begin;
}
int getNewBalance(){
return this.new_balance;
}
String getName(){
return this.name;
}
int getTotCharge(){
return this.tot_charge;
}
void setName(String name){
this.name=name;
}
void setBalance(int balance){
this.balance_begin=balance;
}
void setAccno(int acc_no){
this.acc_no=acc_no;
}
void buyProduct(int cost){
this.tot_charge+=cost;
if(cost>=500){
this.credit+=(int)((cost*5)/100);
}
}
void getNewBal(){
int cost=this.tot_charge-this.credit;
if(cost>this.balance_begin){
System.out.println("Balance is not sufficient at the end of month");
}
else{
this.new_balance=this.balance_begin-cost;
}
}
}

3. ii) TestCustomer.java
public class TestCustomer {
public sta c void main(String[] args) {
Customer c=new Customer(1000,5000,"razeen");
System.out.println("The account number is: "+c.getAccno());
System.out.println("The name of the customer is : "+c.getName());
System.out.println("The balance in the beginning of the month :"+c.getBeginBalance());
c.buyProduct(500);
c.buyProduct(400);
c.buyProduct(1000);
c.buyProduct(2000);
c.getNewBal();
System.out.println(c.getNewBalance());
}
}

4. i) Employee.java

abstract class Employee {


int salary;
sta c int increment=5;
abstract int calculate();
}
class DailyWorker extends Employee{
int hours;
int tot_hours;
int salary;
DailyWorker(int hours){
this.hours=hours;
this.tot_hours+=hours;
}
int getHours(){
return this.hours;
}
int getTotHourse(){
return this.tot_hours;
}
void setHour(int hour){
this.hours=hour;
}
void addHour(int hour){
this.tot_hours+=hour;
}
int calculate(){
if(this.tot_hours>=40){
salary+=(tot_hours*3000);
int val=(int)(salary*increment)/100;
salary+=val;
}
else{
this.salary+=tot_hours*3000;
}
return this.salary;
}
}
class MonthlyWorker extends Employee{
int months;
int tot_years;
int salary;
MonthlyWorker(int month){
this.months+=month;
if(this.months>=12){
this.tot_years+=this.months%12;
this.months=this.months%12;
}
}
int getMonths(){
return this.months;
}
int getTotYears(){
return this.tot_years;
}
void setMonth(int month){
this.months=month;
}
void addMonth(int month){
this.months+=month;
if(this.months>12){
this.tot_years+=1;
this.months=this.months%12;
}
this.tot_years+=month;
}
int calculate(){
if(this.tot_years>=5){
salary+=(tot_years*100000);
int val=(int)(this.increment*salary)/100;
salary+=val;
}
else{
this.salary+=tot_years*3000;
}
return this.salary;
}
}

4.
ii)EmployeeTest.java

public class EmployeeTest {


public sta c void main(String[] args) {
DailyWorker emp1=new DailyWorker(100);
MonthlyWorker emp2=new MonthlyWorker(30);
System.out.println("The total salary of the DailyWorker is: "+emp1.calculate());
System.out.println("The total salary of the MonthlyWorker is: "+emp2.calculate());
}
}

5.
i)Invoice.java

public class Invoice {


int prod_no;
String prod_name;
int quan ty;
int price;
int invoice_id;
public Invoice(int invoice_id,int prod_no,String prod_name,int price,int quan ty){
this.invoice_id=invoice_id;
this.prod_name=prod_name;
this.prod_no=prod_no;
if(price<0){
this.price=0;
}
else{
this.price=price;
}
if(quan ty<0){
this.quan ty=0;
}
else{
this.quan ty=quan ty;
}
}
String getProdName(){
return this.prod_name;
}
int getQuan ty(){
return this.quan ty;
}
int getPrice(){
return this.price;
}
int getInvoice(){
return this.invoice_id;
}
int getProdid(){
return this.prod_no;
}
void setPrice(int c){
if(c>0){
this.price=c;
}
}
void setQuan ty(int q){
if(q>0){
this.quan ty=q;
}
}
int getTotalPrice(){
return this.price*this.quan ty;
}
}

5.
ii)InvoiceTest.java

public class InvoiceTest {


public sta c void main(String[] args) {
Invoice in1=new Invoice(1,101,"Mouse",2000,2);
Invoice in2=new Invoice(2,102,"Keyboard",1000,3);
System.out.println("Invoice_id\tproduct_id\tproduct_name\t\tprice\t\tquan ty\t\ otalprice");
System.out.println(in1.getInvoice()+"\t\t"+in1.getProdid()+"\t\t"+in1.getProdName()+"\t\t\t"+in1.getPrice()+"\t\t"+in1
+in1.getTotalPrice());
System.out.println(in2.getInvoice()+"\t\t"+in2.getProdid()+"\t\t"+in2.getProdName()+"\t\t"+in2.getPrice()+"\t\t"+in2
n2.getTotalPrice());
}
}

6.TestDate.java
class Date {
int date;
int month;
int year;
Date(int date, int month, int year) {
this.year = year;
if (month <= 12) {
if (month == 2) {
if (this.year % 400 == 0 || (this.year % 4 == 0 && this.year % 100 != 0)) {
System.out.println(month);
if (date <= 29) {
this.date = date;
this.month=month;
} else {
this.date = -1;
this.month=-1;
}
} else {
if (date <= 28) {
this.date = date;
this.month=month;
} else {
this.date = -1;
this.month=-1;
}
}
} else if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10
|| month == 12) {
if (date <= 31) {
this.date = date;
this.month=month;
} else {
this.date = -1;
this.month=-1;
}
} else {
if (date <= 30) {
this.date = date;
this.month=month;
} else {
this.date = -1;
this.month=-1;
}
}
} else {
this.month = -1;
this.date = -1;
}
}
int getDate() {
return this.date;
}
int getMonth() {
return this.month;
}
int getYear() {
return this.year;
}
void setDate(int date) {
if (this.month == 2) {
if (this.year % 400 == 0 || (this.year % 4 == 0 && this.year % 100 != 0)) {
if (date <= 29) {
this.date = date;
} else {
System.out.println("can't insert the date beacuse it is a leap year");
}
} else {
if (date <= 28) {
this.date = date;
} else {
System.out.println("can't insert the date beacuse it is not a leap year");
}
}
}
else if (this.month == 1 || this.month == 3 || this.month == 5 || this.month == 7 || this.month == 8
|| this.month == 10 || this.month == 12) {
if (date <= 31) {
this.date = date;
} else {
System.out.println("can't set date becuase it exceeds the allocated date for a month");
}
}
else{
if (date <= 30) {
this.date = date;
} else {
System.out.println("can't set date becuase it exceeds the allocated date for a month");
}
}
}
void setMonth(int month){
if(month>=1 && month<=12){
this.month=month;
}
else{
System.out.println("can't set month");
}
}
void setYear(int year){
this.year=year;
}
void displayDate(){
System.out.println("The objects date is :");
System.out.println(this.month+"/"+this.date+"/"+this.year);
System.out.println(this.month+"-"+this.date+"-"+this.year);
System.out.println(this.month+":"+this.date+":"+this.year);
System.out.println(this.month+"."+this.date+"."+this.year);
}
}
public class TestDate {
public sta c void main(String[] args) {
Date d=new Date(4,2,2001);
System.out.println("The date is :");
System.out.println(d.getDate());
System.out.println(d.getMonth());
System.out.println(d.getYear());
d.setDate(30);
d.setMonth(2);
d.setYear(2011);
d.displayDate();
}
}

You might also like