System.out.print("Model is: ")

advertisement
1.
A washing machine class with methods as switchOn, acceptClothes, Create acceptDetergent,
switchOff. acceptClothes accepts the noofClothes as argument & returns the noofClothes
import java.util.*;
class WashingMachine
{
Scanner input=new Scanner(System.in);
public void switchOn ()
{
System.out.println ("The lid is open.");
}
public void start ()
{
System.out.println ("Start washing ...");
}
public void acceptDetergent ()
{
System.out.println("Adding Detergent.. ");
start();
}
public int acceptClothes()
{
System.out.println("Enter no of clothes: ");
int no=input.nextInt();
return no;
}
public void switchOff ()
{
System.out.println ("The lid is closed.");
}
public static void main(String[] args)
{
WashingMachine wm=new WashingMachine();
wm.switchOn();
int numOFClothes=wm.acceptClothes();
wm.acceptDetergent();
wm.switchOff();
System.out.println(numOFClothes+" clothes get washed");
}
}
2. Create a calculator class which will have methods add, multiply, divide & subtract
class Calculation
{
public float add(float a,float b)
{
return a+b;
}
public float subtract(float a,float b)
{
return a-b;
}
public float multiply(float a,float b)
{
return a*b;
}
public float divide(float a,float b)
{
return a/b;
}
}
public class Calculator
{
public static void main(String []args)
{
Calculation cal=new Calculation();
float add=cal.add(5,10);
float sub=cal.subtract(5,10);
float mul=cal.multiply(5,10);
float 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 student 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 nameAndResult=data.getName();
System.out.println(nameAndResult);
}
}
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 of amount & balance. Similarly, the
withdraw method would accept the attributes amount & balance & returns the new balance ‘balance –
amount’ if balance > = amount or return 0 otherwise.
import java.util.*;
import java.io.*;
import java.lang.*;
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 bal;
}
int withdraw(int amt) {
if (bal < amt) {
System.out.println("Not sufficient balance.");
return 0;
}
if (amt < 0) {
System.out.println("Invalid Amount");
return 0;
}
bal = bal - amt;
return bal;
}
}
public class Bank
{
public static void main(String[]args)
{
Customer Cust=new Customer(1500);
Scanner input=new Scanner(System.in);
System.out.println("Enter the amount to deposit:");
int dep=input.nextInt();
int bal1=Cust.deposit(dep);
System.out.println("net bal: "+bal1);
System.out.println("Enter the amount to withdraw:");
int with=input.nextInt();
int bal2=Cust.withdraw(with);
System.out.println("net bal: "+bal2);
}
}
5. Create an Employee class which has methods netSalary which would accept salary & tax as arguments
& returns the netSalary which is tax deducted from the salary. Also it has a method grade which would
accept the grade of the employee & return grade.
import java.util.*;
class Employee
{
static Scanner input=new Scanner(System.in);
public double netSalary(double salary, double taxrate)
{
double tax=salary*taxrate;
double netpay=salary-tax;
return netpay;
}
public static String grade( ){
System.out.print("Enter Grade: ");
String grade=input.next();
return grade;
}
public static void main(String[] args){
Employee emp=new Employee();
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);
String g=emp.grade();
System.out.println("Grade is: "+g);
}
}
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.
import java.util.*;
class Product{
int productID;
String name;
int categoryID;
double price;
Product(int productID,String name,int categoryID,double price){
this.productID=productID;
this.name=name;
this.categoryID=categoryID;
this.price=price;
}
}
class ElectricalProduct extends Product{
int voltageRange;
int wattage;
ElectricalProduct(int productID,String name,int categoryID,double price,int voltageRange, int
wattage)
{
super(productID,name,categoryID,price);
this.voltageRange=voltageRange;
this.wattage=wattage;
}
public void display(){
System.out.println("Product ID: "+productID);
System.out.println("Name: "+name);
System.out.println("Category ID: "+categoryID);
System.out.println("Price: "+price);
System.out.println("Voltage Range: "+voltageRange);
System.out.println("Wattage: "+wattage);
}
public static void main(String[] args)
{
Scanner input=new Scanner(System.in);
System.out.println("Enter Product ID: ");
int pid=input.nextInt();
System.out.println("Enter Name: ");
String name=input.next();
System.out.println("Enter Catagory ID: ");
int cid=input.nextInt();
System.out.println("Enter Price: ");
double price=input.nextDouble();
System.out.println("Enter Voltage Range: ");
int vrange=input.nextInt();
System.out.println("Enter Wattage: ");
int wattage=input.nextInt();
System.out.println("****Details of Electrical Product****");
System.out.println();
ElectricalProduct p=new ElectricalProduct(pid,name,cid,price,vrange,wattage);
p.display();
System.out.println("enter new value for price");
double new_price=input.nextDouble();
System.out.println("enter new value for wattage");
int new_wattage=input.nextInt();
System.out.println("****Updated details of Electrical Product****");
System.out.println();
ElectricalProduct p1=new ElectricalProduct(pid,name,cid,new_price,vrange,new_wattage);
p1.display();
}
}
7. Create Book having following attributes: Book ID, Title, Author and Price. Create Periodical which has
the following additional attributes: Period (weekly, monthly etc...) .Add a behavior to modify the Price and
the Period of the periodical. Display the updated periodical details.
import java.util.*;
class Book
{
int BookID;
String Author;
Double Price;
Book(int BookID,String Author,Double Price)
{
this.BookID=BookID;
this.Author=Author;
this.Price=Price;
}
}
class Periodical extends Book{
String Period;
Periodical(int BookID,String Author,Double Price,String Period)
{
super( BookID,Author,Price);
this.Period=Period;
}
void show(){
System.out.println("BookID = " +BookID);
System.out.println("Author = " +Author);
System.out.println("Price = " +Price);
System.out.println("Period = " + Period);
}
public static void main(String[] args)
{
Scanner input=new Scanner(System.in);
System.out.println("book id: ");
int BookID=input.nextInt();
System.out.println("author name: ");
String Author=input.next();
System.out.println("Price: ");
Double Price=input.nextDouble();
System.out.println("Period: ");
String Period=input.next();
Periodical pe=new Periodical(BookID,Author,Price,Period);
System.out.println("****book details****");
System.out.println();
pe.show();
// behaviou for price and period
System.out.println("enter new value for price");
Double newprice=input.nextDouble();
System.out.println("enter new value for period");
String newperiod=input.next();
Periodical pe1=new Periodical(BookID,Author,newprice,newperiod);
System.out.println("**** updated book Details****");
System.out.println();
pe1.show();
}
}
8. Create Vehicle having following attributes: Vehicle No., Model, Manufacturer and Color. Create truck
which has the following additional attributes:loading capacity( 100 tons…).Add a behavior to change the
color and loading capacity. Display the updated truck details.
import java.util.*;
class Vehicle
{
int no;
String model;
String manufacturer;
String color;
Vehicle(int no,String model,String manufacturer,String color){
this.no=no;
this.model=model;
this.manufacturer=manufacturer;
this.color=color;
}
}
class Truck extends Vehicle{
int capacity;
Truck(int no,String model,String manufacturer,String color,int capacity){
super( no, model, manufacturer, color);
this.capacity=capacity;
}
void show(){
System.out.println("No = " + no);
System.out.println("Model = " + model);
System.out.println("manufacturer = " + manufacturer);
System.out.println("Color = " + color);
System.out.println("Capacity = " + capacity);
}
public static void main(String[] args)
{
Scanner input=new Scanner(System.in);
System.out.println("Truck No: ");
int no=input.nextInt();
System.out.println("Model: ");
String model=input.next();
System.out.println("Manufacturer: ");
String manufacturer=input.next();
System.out.println("Color: ");
String color=input.next();
System.out.println("Loading Capacity: ");
int cap=input.nextInt();
Truck t=new Truck(no,model,manufacturer,color,cap);
System.out.println("****Truck Details****");
System.out.println();
t.show();
// behaviou for color and capacity
System.out.println("enter new value for color");
String newcolor=input.next();
System.out.println("enter new value for capacity");
int newcap=input.nextInt();
Truck t1=new Truck(no,model,manufacturer,newcolor,newcap);
System.out.println("**** updated Truck Details****");
System.out.println();
t1.show();
}
}
9. Write a program which performs to raise a number to a power and returns the value. Provide a behavior
to the program so as to accept any type of numeric values 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 category of car customer
is looking for and returns the car Model available in that category. the function should accept the following
categories "SUV", "SEDAN", "ECONOMY", and "MINI" which in turn returns "TATA SAFARI" ,
"TATA INDIGO" , "TATA INDICA" and "TATA NANO" respectively.
import java.util.*;
class TataMotors{
String category;
String model;
TataMotors(String category,String model){
this.category=category;
this.model=model;
}
public String getCategory(){
return category;
}
public String getModel(){
return model;
}
public static void ModelOfCategory(ArrayList<TataMotors> list){
Scanner input=new Scanner(System.in);
System.out.print("Enter Category: ");
String category=input.nextLine();
System.out.println();
System.out.print("Model is: ");
for (TataMotors tm : list){
if(tm.getCategory().equals(category)){
System.out.print(tm.getModel());
}
}
}
public static void main(String[] args)
{
ArrayList<TataMotors> list=new ArrayList<TataMotors>();
list.add(new TataMotors("SUV","TATA SAFARI"));
list.add(new TataMotors("SEDAN","TATA INDIGO"));
list.add(new TataMotors("ECONOMY","TATA INDICA"));
list.add(new TataMotors("MINI","TATA NANO"));
ModelOfCategory(list);
}
}
Download