Zim's Life in TCS Trivandrum! | Yet another ILP story for ...



NTRODUCTION TO BASICS OF JAVA PROGRAMMING

ASSIGNMENT I

 

1. Write a program to find the difference between sum of the squares and the square of the sums of n numbers

 

/*SUM OF SQUARES-SQUARE OF SUM OF N NUMBERS*/

import java.util.*;

class ssmss

{

public static void main(String[] args) 

{

int sum=0,sum1=0,sum2=0;

Scanner input=new Scanner(System.in);

System.out.println("Enter value of n: ");

int n=input.nextInt();

for(int i=1;i=50)

{return "passed";

}

else return "failed";

}

public String Inputname(String str)

{

name=str;

return name;

}

}

class average

{

public static void main(String args[])

{Scanner s=new Scanner(System.in);

System.out.println("enter student name: ");

String n=s.nextLine();

System.out.println("enter marks of first subject: ");

double num1=s.nextDouble();

System.out.println("enter marks of second subject: ");

double num2=s.nextDouble();

System.out.println("enter marks of third subject: ");

double num3=s.nextDouble();

Student st=new Student();

 

String g=st.average(num1,num2,num3);

System.out.println(st.Inputname(n)+" is "+g);

}

}

 

Output:

enter student name:

silla subhangi

enter marks of first subject:

78

enter marks of second subject:

56

enter marks of third subject:

90

silla subhangi is passed

—————————————————————————————————————————————

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.

Ans:

 

import java.util.*;

import java.io.*;

class Bank{

public double deposit(double amt,double bal)

{

return (amt+bal);

}

public double withdraw(double amt,double bal)

{

if(bal>=amt)

return (bal-amt);

else return 0;

}

 

}

class atm

{

public static void main(String args[])

{

Scanner s= new Scanner(System.in);

Bank b=new Bank();

System.out.println("enter the amount to deposit");

double a=s.nextDouble();

double bal=0;

double newbal=b.deposit(a,bal);

 

System.out.println("amount is deposited");

System.out.println("The total balance is: "+newbal);

System.out.println("enter the amount to withdraw");

double am=s.nextDouble();

double ba=b.withdraw(am,newbal);

if(ba==0){System.out.println("no sufficient balance to withdraw");}

else{

System.out.println("the amount is withdrawn");

 

System.out.println("the balance is "+ba);

}

 

}

}

 

Output:

enter the amount to deposit

200

amount is deposited

The total balance is: 200.0

enter the amount to withdraw

100

the amount is withdrawn

the balance is 100.0

——————————————————————————————————————————

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.

Ans:

 

import java.util.*;

import java.io.*;

class Employee{

public double netsalary(double sal,double tax)

{

return (sal-(sal*tax/100));

}

public String grade(String empgrade){

return empgrade;

 

}

}

class tax

{public static void main(String args[])

{

 

Scanner s = new Scanner(System.in);

System.out.println("enter grade of employee");

String str=s.nextLine();

System.out.println("enter the salary ");

double sa=s.nextDouble();

System.out.println("enter tax in percentage");

double t=s.nextDouble();

Employee e=new Employee();

double finsal=salary(sa,t);

System.out.println("The net salary of "+e.grade(str)+" grade employee is: "+finsal);

 

}

}

Output:

 

enter grade of employee

A

enter the salary

26000

enter tax in percentage

15

The net salary of A grade employee is: 22100.0

———————————————————————————————————————————

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.

Ans:

 

import java.util.*;

import java.io.*;

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;

 

}

public void display1()

{

System.out.println("product id= "+productID);

System.out.println("product name= "+name);

System.out.println("category id= "+categoryID);

System.out.println("price= "+price);

}

}

class ElectricalProduct extends Product{

double voltageRange;

double wattage;

ElectricalProduct(int productID,String name,int categoryID,double price,double voltageRange,

double wattage){

super(productID,name,categoryID,price);

this.voltageRange=voltageRange;

this.wattage=wattage;

}

public void display()

{

super.display1();

System.out.println("voltage range= "+voltageRange+" Volts”);

System.out.println("wattage= "+wattage+ "watts");

}

}

class pro

{

public static void main(String args[])

 

{

ElectricalProduct ep=new ElectricalProduct(1,”bulb”,2,80,20,60);

ep.display();

}

}

Output:

 

product id= 1

product name= bulb

category id= 2

price= 80.0

voltage range= 20.0 Volts

wattage= 60.0watts

 

————————————————————————————————————————————

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.

Ans:

 

import java.util.*;

class Book{

int BookID=1;;

double Price;

String Title="java";String Author="Herbit Schildt";

}

class Periodical extends Book{

String Period;

public void display(){

System.out.println("Book id = "+(BookID));

System.out.println("\nprice="+(Price));

System.out.println("\ntitle is "+(Title));

System.out.println("\nauthor is "+(Author));

System.out.println("\nperiod="+(Period));

}

}

class mag

{public static void main(String args[]){

Scanner s=new Scanner(System.in);

Periodical p=new Periodical();

System.out.println("Enter the period of book : ");

p.Period= s.nextLine ();

System.out.println("Enter the price of book : ");

p.Price=s.nextInt();

p.display();

}}

 

Output:

Enter the period of book :

yearly

Enter the price of book :

900

Book id = 1

price=900.0

title is java

author is Herbit Schildt

 

period=yearly

————————————————————————————————————————————-

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.

Ans:

 

import java.util.*;

class Vehicle{

int vehnum=1;

String model="XYz13",manfr="tata",color;

}

class Truck extends Vehicle{

String capacity;

public void display(){

System.out.println("vehicle number = "+vehnum);

System.out.println("\nmodel="+model);

System.out.println("\nmanufacturer= "+manfr);

System.out.println("\ncolor is "+color);

System.out.println("\nloading capacity="+capacity);

}

}

class vehtruck

{public static void main(String args[]){

Scanner s=new Scanner(System.in);

Truck p=new Truck();

System.out.println("Enter the color : ");

p.color= s.nextLine ();

System.out.println("Enter the loading capacity: ");

p.capacity=s.nextLine();

p.display();

}}

Output:

Enter the color :

white

Enter the loading capacity:

200 tonnes

vehicle number = 1

model=XYz13

manufacturer= tata

color is white

 

loading capacity=200 tonnes

———————————————————————————————————————————

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.

Ans:

import java.util.*;

 

class poWer

{

public static void main(String[] args)

{

 

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: "+(value));

}

}

Output:

 

Enter Number: 2

Raise Number’s power: 3

Result is: 8.0

 

———————————————————————————————————————————-

10)Write a function Model-of-Category for a Tata motor dealers, which accepts category of car customer

 

islooking 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.

Ans:

 

import java.util.*;

class clrt{

static String Model_of_category(String Category){

String cat[]={“SUV”,”SEDAN”,”ECONOMY”,”MINI”};

String model[]={“TATA SAFARI”,”TATA INDIGO”,”TATA INDICA”,”NANO”};

String str=”";

for(int i=0;i ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download