You are on page 1of 5

1.

Create a washing machine class with methods as switchOn, acceptClothes, accep


tDetergent, switchOff. acceptClothes accepts the noofClothes as argument & retur
ns the noofClothes
2. Create a calculator class which will have methods add, multiply, divide & sub
tract?
class Calculation{
public int add(int a,int b){
return a+b;
}
public int subtract(int a,int b){
if(a>b){
return a-b;
}
else{
return b-a;
}
}
public int multiply(int a,int b){
return a*b;
}
public int divide(int a,int b){
if(a>b){
return a/b;
}
else{
return b/a;
}
}
}
public class Calculator{
public static void main(String []args){
Calculation cal=new Calculation();
int add=cal.add(5,10);
int sub=cal.subtract(5,10);
int mul=cal.multiply(5,10);
int div=cal.divide(5,10);
System.out.println(add);
System.out.println(sub);
System.out.println(mul);
System.out.println(div);
}

3. Create a class called Student which has the following methods:


i. Average: which would accept marks of 3 examinations & return whether the stud
ent has passed or failed depending on whether he has scored an average above 50
or not.
ii. Inputname: which would accept the name of the student & returns the name.?
import java.util.*;
public class Student{
Scanner input=new Scanner(System.in);
public String average(){
System.out.print("Enter Marks1: ");
double m1=input.nextDouble();
System.out.print("Enter Marks2: ");
double m2=input.nextDouble();
System.out.print("Enter Marks3: ");
double m3=input.nextDouble();
double tm=m1+m2+m3;
double avg=tm/3;
if(avg<50){
return "Failed";
}
if(avg>50){
return "Passed";
}
return " ";
}
public String getName(){
System.out.println("Enter Name:");
String name=input.nextLine();
String result=average();
return name+" get "+result;
}
public static void main(String[]args){
Student data=new Student();
String nameAndResut=data.getName();
System.out.println(nameAndResut);
}
}
4. Create a Bank class with methods deposit & withdraw. The deposit method would
accept attributes amount & balance & returns the new balance which is the sum o
f amount & balance. Similarly, the withdraw method would accept the attributes a
mount & balance & returns the new balance balance amount if balance > = amount or
return 0 otherwise.?
import javax.swing.*;
class Customer{
int bal;
Customer(int bal) {
this.bal = bal;
}
int deposit(int amt) {
if (amt < 0) {
System.out.println("Invalid Amount");
return 1;
}
bal = bal + amt;
return 0;
}
int withdraw(int amt) {
if (bal < amt) {
System.out.println("Not sufficient balance.");
return 1;
}
if (amt < 0) {
System.out.println("Invalid Amount");
return 1;
}
bal = bal - amt;
return 0;
}
void check() {
JOptionPane.showMessageDialog(null,"Balance:" + Integer.toString
(bal));
}
}
public class Bank{
public static void main(String[]args){
Customer Cust=new Customer(1500);
String st1=JOptionPane.showInputDialog(null,"Enter the amount to deposit
:");
int dep=Integer.parseInt(st1);
int bal1=Cust.deposit(dep);
Cust.check();
String st2=JOptionPane.showInputDialog(null,"Enter the amount to withdra
w:");
int with=Integer.parseInt(st2);
int bal2=Cust.withdraw(with);
Cust.check();
}
}
5. Create an Employee class which has methods netSalary which would accept salar
y & tax as arguments & returns the netSalary which is tax deducted from the sala
ry. Also it has a method grade which would accept the grade of the employee & re
turn grade.?
import java.util.*;
class Employee
{
public double netSalary(double salary, double taxrate){
double tax=salary*taxrate;
double netpay=salary=tax;
return netpay;
}
public static void main(String[] args)
{
Employee emp=new Employee();
Scanner input=new Scanner(System.in);
System.out.print("Enter Salary: ");
double sal=input.nextDouble();
System.out.print("Enter Tax in %: ");
double taxrate=input.nextDouble()/100;
double net=emp.netSalary(sal,taxrate);
System.out.println("Net Salary is: "+net);
}
}
6. Create Product having following attributes: Product ID, Name, Category ID and
UnitPrice. Create ElectricalProduct having the following additional attributes:
VoltageRange and Wattage. Add a behavior to change the Wattage and price of the
electrical product. Display the updated ElectricalProduct details.
7. Create Book having following attributes: Book ID, Title, Author and Price. Cr
eate Periodical which has the following additional attributes: Period (weekly, m
onthly etc...) .Add a behavior to modify the Price and the Period of the periodi
cal. Display the updated periodical details.?
import java.util.*;
class Book{
int id;
String title;
String author;
double price;

public void setId(int id){


this.id=id;
}
public int getId(){
return id;
}
public void setTitle(String title){
this.title=title;
}
public String getTitle(){
return title;
}
public void setAuthor(String author){
this.author=author;
}
public String getAuthor(){
return author;
}
public void setPrice(double price){
this.price=price;
}
public double getPrice(){
return price;
}
}
class Periodical
{
String timeperiod;
public void setTimeperiod(String timeperiod){
this.timeperiod=timeperiod;
}
public String getTimeperiod(){
return timeperiod;
}
}
public class BookInformation{
public static void main(String[]args){
Book b=new Book();
Periodical p=new Periodical();
Scanner input=new Scanner(System.in);
System.out.print("Book ID: ");
int id=input.nextInt();
b.setId(id);
System.out.print("Title: ");
String title=input.next();
b.setTitle(title);
System.out.print("Author: ");
String author=input.next();
b.setAuthor(author);
System.out.print("Price: ");
double price=input.nextDouble();
b.setPrice(price);
System.out.print("Period: ");
String pp=input.next();
p.setTimeperiod(pp);
System.out.println();
System.out.println("----Book Information----");
System.out.println();
System.out.println("Book ID: "+b.getId());
System.out.println("Title: "+b.getTitle());
System.out.println("Author: "+b.getAuthor());
System.out.println("Price: "+b.getPrice());
System.out.println("Period: "+p.getTimeperiod());
}
}
8. Create Vehicle having following attributes: Vehicle No., Model, Manufacturer
and Color. Create truck which has the following additional attributes:loading ca
pacity( 100 tons ).Add a behavior to change the color and loading capacity. Displa
y the updated truck details.
9. Write a program which performs to raise a number to a power and returns the v
alue. Provide a behavior to the program so as to accept any type of numeric valu
es and returns the results.
import java.util.*;
import java.text.*;
class NumberProgram
{
public static void main(String[] args)
{
DecimalFormat df=new DecimalFormat("##.##");
Scanner input=new Scanner(System.in);
System.out.print("Enter Number: ");
double num=input.nextDouble();
System.out.print("Raise Number's power: ");
double pow=input.nextDouble();
double value=Math.pow(num,pow);
System.out.println("Result is: "+df.format(value));
}
}
10. Write a function Model-of-Category for a Tata motor dealers, which accepts c
ategory of car customer is looking for and returns the car Model available in th
at category. the function should accept the following categories "SUV", "SEDAN",
"ECONOMY", and "MINI" which in turn returns "TATA SAFARI" , "TATA INDIGO" , "TA
TA INDICA" and "TATA NANO" respectively.

You might also like