0% found this document useful (0 votes)
6 views8 pages

Chai

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)
6 views8 pages

Chai

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/ 8

Exercise – 2

Numbers.java:

import java.util.*;

public class Numbers{


public static int Smallest(ArrayList<Integer> a){
Collections.sort(a);
return a.get(0);
}
public static int Greatest(ArrayList<Integer> a){
Collections.sort(a);
return a.get(a.size() - 1);
}
public static int Sum(ArrayList<Integer> a){
int sum = 0;
for(int c : a){
sum += c;
}
return sum;
}
public static double Avg(ArrayList<Integer> a){
int n = a.size();
int sum = Sum(a);
return sum / n;
}
public static void main(String[] args){
int e = 0;
Scanner sc = new Scanner(System.in);
ArrayList<Integer> a = new ArrayList<>();

while(true){
e = sc.nextInt();
if(e == -1) break;
a.add(e);
}

int s = Smallest(a);
int g = Greatest(a);
int ss = Sum(a);
double av = Avg(a);

System.out.println("Smallest : "+s);
System.out.println("Greatest : "+g);
System.out.println("Sum : "+ss);
System.out.println("Average : "+av);
}
}
----------------------------------------------------------------------------------------------------------------------------------------
DateTest.java:

import java.util.*;

public class DateTest{


public static void main(String[] args){
Date d = new Date(22, 12, 2004);
Date c = new Date(12, 12, -2004);

Date e = new Date(02, 27, 2022);


e.DispDate();
}
}

class Date{
private int m;
private int d;
private int y;
private boolean leap;

Date(int m, int d, int y){


if(((y % 400 == 0) || (y % 100 != 0 && y % 4 == 0)) && y > 0){
this.y = y;
this.leap = true;
}
else if(y < 0) System.out.println("Wrong Year");
else{
this.leap = false;
}

if(m > 0 && m <13){


this.m = m;
}
else System.out.println("Wrong Month");

if((m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12) && (d > 0) && (d < 32)) {


this.d = d;
}
else if ((m == 2)){
if(leap && d < 30 && d > 0) this.d = d;
else if(!leap && d > 0 && d < 29) this.d = d;
}
else{
this.d = d;
}
}
public int GetDate(){
return this.d;
}

public int GetMonth(){


return this.m;
}

public int GetYear(){


return this.y;
}

public void DispDate(){


System.out.println(m+"/"+d+"/"+y);
System.out.println(m+"-"+d+"-"+y);
System.out.println(m+"."+d+"."+y);
}

-----------------------------------------------------------------------------------------------------------------------------------------

AccountTest.java:

import java.util.*;

public class AccountTest{


public static void main(String[] args){
Account a = new Account();
a.setacc(5013);
a.setname("Chai");
a.setbal(2000);
a.getacc();
a.getname();
a.getbal();
a.Deposit(1500);
a.getbal();
a.withdraw(3650);
a.getbal();
}
}

class Account{
int acc;
String name;
int bal;

public void setacc(int acc){


if(acc > 0) this.acc = acc;
else System.out.println("Account Number should be positive");
}

public void setname(String name){


this.name = name;
}

public void setbal(int bal){


if(bal > 0) this.bal = bal;
else System.out.println("Account balance should be positive");
}

public void getacc(){


System.out.println("Account Number : "+acc);
}

public void getname(){


System.out.println("Customer Name : "+name);
}

public void getbal(){


System.out.println("Account balance : "+bal);
}

public void withdraw(int amt){


if(bal - amt < 0) System.out.println("Not Sufficient Balance");
else{
bal -= amt;
System.out.println("Withdrawn Rs."+amt);
}
}

public void Deposit(int amt){


if(amt < 0) System.out.println("Amount should be greater than Rs.0");
else{
bal += amt;
System.out.println("Deposited Rs."+amt);
}
}}
CreditTest.java:

import java.util.*;

public class CreditTest{


public static void main(String[] args){
Credit a = new Credit();
a.setacc(5013);
a.setname("Chai");
a.setbal(5000.00);
a.getacc();
a.getname();
a.getbal();
a.buy(1500);
a.New();
a.getbal();
a.buy(2750);
a.New();
a.getbal();
}
}

class Credit{
int acc;
String name;
double bal;
double credit;
double charge;

public void setacc(int acc){


if(acc > 0) this.acc = acc;
else System.out.println("Account Number should be positive");
}

public void setname(String name){


this.name = name;
}

public void setbal(double bal){


if(bal > 0) this.bal = bal;
else System.out.println("Account balance should be positive");
}

public void getacc(){


System.out.println("Account Number : "+acc);
}

public void getname(){


System.out.println("Customer Name : "+name);
}

public void getbal(){


System.out.println("Account balance : "+bal);
}

public void buy(double amt){


this.charge+=amt;
if(amt > 500){
credit += (int) amt*0.1;
}
}

public void New(){


if(charge - credit > bal) System.out.println("Credit limit exceeded");
else {
bal = bal - charge + credit;
charge = 0;
credit = 0;
}
}
}

---------------------------------------------------------------------------------------------------------------------------------------
InvoiceTest.java:

import java.util.*;

public class InvoiceTest{


public static void main(String[] args){
Invoice a = new Invoice();
Invoice b = new Invoice();

a.setpno(-2);
a.setpname("Hamam");
a.setqt(0);
a.setprice(-23);

b.setpno(10);
b.setpname("Dettol");
b.setqt(20);
b.setprice(75.45);
b.getamt();
}
}

class Invoice{
private int pno;
private String pname;
private int qt;
private double price;
public void setpno(int pno){
if(pno > 0) this.pno = pno;
else System.out.println("Product Number should be positive");
}

public void setpname(String pname){


this.pname = pname;
}

public void setqt(int qt){


if(pno > 0) this.qt = qt;
else System.out.println("Product quantity should be greater than 0");
}

public void setprice(double price){


if(pno > 0) this.price = price;
else System.out.println("Product price should be greater than 0.00");
}

public void getpno(){


System.out.println("Product Number : "+pno);
}

public void getpname(){


System.out.println("Product Name : "+pname);
}

public void getprice(){


System.out.println("Product Price : "+price);
}

public void getqt(){


System.out.println("Product Quantity : "+qt);
}

public void getamt(){


System.out.println("Total amt : "+qt*price);
}

-------------------------------------------------------------------------------------------------------------------------------------
Employee.java:

public class Employee{


public static void main(String[] args){
Temp t1= new Temp("Chai",13000,4);
Perm p1 = new Perm("Ganesh",5000,60);
System.out.println("Weekly Employee Salary: "+t1.getsal());
System.out.println("Permanetn Employee Salary: "+p1.getsal());
}
}
class Temp{
private String name;
private int sal;
private int hrs;
public Temp(String n,int s,int h){
name=n;
sal=s;
hrs=h;
}
public void setname(String n){name=n;}
public void sethrs(int h){hrs=h;}
public String getname(){return name;}
public int getsal(){
if(hrs>40){
sal+= 500;
return sal;
}
else{
return sal;
}
}
}
class Perm{
private String name;
private int sal;
private int yrs;
public Perm(String n,int s,int h){
name=n;
sal=s;
yrs=h;
}
public void setname(String n){name=n;}
public void setyrs(int h){yrs=h;}
public String getname(){return name;}
public int getsal(){
if(yrs>5){
sal+= (int) (0.05*sal);
return sal;
}
else{
return sal;
}
}}

You might also like