1) java final variable - web viewwhat java api says about out object, the same is said with err...

23
INTERNET PROGRAMMING SCHEME OF EVALUATION 1.A Class circle with variables and methods – 3M class TestCircle with main -2M 1.B using System.in— 1M using System.out— 1M using System.err— 1M 1.C program handling IOException - 2M 1.D Class Rectangle with variables and methods – 3M class TestRectanglewith main - 2M 2.A abstract class creation with constructor and abstract methods -2M class Pepco with variables and methods -2M class testDrink with main -1M 2.B Using scanner class- 1M reading all variables using scanner class methods - 1M Calculation of TOTALE SAL -3M 2.C abstract class Vehicle with varibales and methods – 2 M class Truck and its methods -2M class TestVehicle with main -1M

Upload: vannhu

Post on 07-Mar-2018

217 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: 1) Java final variable -    Web viewWhat Java API says about out object, the same is said with err object also

INTERNET PROGRAMMING

SCHEME OF EVALUATION

1.A Class circle with variables and methods – 3M class TestCircle with main -2M

1.B using System.in— 1M

using System.out— 1Musing System.err— 1M

1.C program handling IOException - 2M

1.D Class Rectangle with variables and methods – 3M class TestRectanglewith main -2M

2.A abstract class creation with constructor and abstract methods

-2Mclass Pepco with variables and methods -2Mclass testDrink with main -1M

2.B

Using scanner class- 1Mreading all variables using scanner class methods - 1MCalculation of TOTALE SAL -3M

2.Cabstract class Vehicle with varibales and methods – 2 M class Truck and its methods -2Mclass TestVehicle with main -1M

3.A class Person with variables and methods- 1M class student with variables and methods- 1M class teacher with variables and methods- 1M class collegestudent with variables and methods- 1Mclass testhighSchool with main -1M

Page 2: 1) Java final variable -    Web viewWhat Java API says about out object, the same is said with err object also

3.Bdefinition of Exception - 1Mtry ,catch ,throw example -4M

3.Cuses of final keyword -2Mprogram on using final variable -1Mprogram on using final method- 1Mprogram on using final class- 1M

4.A class invoice with variables and methods- 3M class invoicetest with main -2M

4.B Using BufferedReader -2M Reading bank data and displaying - 2M display balance after withdraw and deposit- 1M

4.C program Using interface(s) to demonstrate polymorphism -5M

INTERNET PROGRAMMING

TEST-2 KEY

1.a

/* * The Circle class models a circle with a radius and color. */public class Circle { // Save as "Circle.java" // private instance variable, not accessible from outside this class private double radius; private String color;

Page 3: 1) Java final variable -    Web viewWhat Java API says about out object, the same is said with err object also

// The default constructor with no argument. // It sets the radius and color to their default value. public Circle() { radius = 1.0; color = "red"; } // 2nd constructor with given radius, but color default public Circle(double r) { radius = r; color = "red"; } // A public method for retrieving the radius public double getRadius() { return radius; } // A public method for computing the area of circle public double getArea() { return radius*radius*Math.PI; }}

public class TestCircle { // Save as "TestCircle.java" public static void main(String[] args) { // Declare an instance of Circle class called c1. // Construct the instance c1 by invoking the "default" constructor // which sets its radius and color to their default value. Circle c1 = new Circle(); // Invoke public methods on instance c1, via dot operator. System.out.println("The circle has radius of " + c1.getRadius() + " and area of " + c1.getArea()); // Declare an instance of class circle called c2. // Construct the instance c2 by invoking the second constructor

Page 4: 1) Java final variable -    Web viewWhat Java API says about out object, the same is said with err object also

// with the given radius and default color. Circle c2 = new Circle(2.0); // Invoke public methods on instance c2, via dot operator. System.out.println("The circle has radius of " + c2.getRadius() + " and area of " + c2.getArea()); }}

1.B) System.in System.out System.errSystem.in System.in is nothing but an in stream of OS linked to System class. Using System class, we can divert the in stream going from Keyboard to CPU into our program. This is how keyboard reading is achieved in Java.See the following snippet of code.

1234

  DataInputStream dis = new DataInputStream(System.in);  System.out.println("Enter your name: ");  String str = dis.readLine();  System.out.println("I know your name is " +  str);

System.out public static final PrintStream out: The "standard" output stream. This

stream is already open and ready to accept output data. Typically this stream corresponds to display output or another output destination specified by the host environment or user.

In API, out is declared as a PrintStream object as static and final. The out object is internally connected to the output mechanism of OS. Any data given to System.out, writes to console (DOS prompt).12

  PrintStream ps = new PrintStream(System.out);  ps.println("Hello");

System.errWhat Java API says about out object, the same is said with err object also. public static final PrintStream err: The "standard" error output stream.

This stream is already open and ready to accept output data.err is also an object of PrintStream connected implicitly to output mechanism of OS. As both out and err has the same status, following both statements do the same job.12

  System.out.println("Hello");  System.err.println("Hello");

Page 5: 1) Java final variable -    Web viewWhat Java API says about out object, the same is said with err object also

1.c) public class BufferedReaderDemo {

public static void main(String[] args) throws Exception { InputStream is = null; InputStreamReader isr = null; BufferedReader br = null;

try{ // open input stream test.txt for reading purpose. is = new FileInputStream("c:/test.txt"); // create new input stream reader isr = new InputStreamReader(is); // create new buffered reader br = new BufferedReader(isr); // releases any system resources associated with reader br.close(); // creates error br.read(); }catch(IOException e){ // IO error System.out.println("The buffered reader is closed"); }finally{ // releases any system resources associated if(is!=null) is.close(); if(isr!=null)

Page 6: 1) Java final variable -    Web viewWhat Java API says about out object, the same is said with err object also

isr.close(); if(br!=null) br.close(); } }}

1.Dclass Rectangle{ private float length; private float width; Rectangle() { length=1.0f; width=1.0f; } Rectangle(float l,float w) { length=l; width=w; } public float getLength() { return length;

Page 7: 1) Java final variable -    Web viewWhat Java API says about out object, the same is said with err object also

} public float getWidth() { return width; } public void setLength(float l) { length=l; } public void setWidth(float w) { width=w; } public double getArea() { return length*width; } public double getPerimeter() { return 2*(length+width); } @Override public String toString() {

Page 8: 1) Java final variable -    Web viewWhat Java API says about out object, the same is said with err object also

return "Length="+length +" "+"width="+width; }}public class JavaApplication18 {

public static void main(String[] args) { Rectangle r=new Rectangle(3,4); System.out.println(r.getArea()); System.out.println(r.getPerimeter()); System.out.println(r.toString()); } }

2.a) abstract class Drink{ int serialno; String desc; abstract String getIngredients(); }class Pepco extends Drink { Pepco()

Page 9: 1) Java final variable -    Web viewWhat Java API says about out object, the same is said with err object also

{ serialno=10; desc="Great Carbonated Drink"; } @Override public String getIngredients() { return "Sugar" +" "+ "Caffeine"; } }

public class TestDrink {

public static void main(String[] args) {

Pepco p= new Pepco();

System.out.println(p.getIngredients());

} }

2.b)import java.util.*;

Page 10: 1) Java final variable -    Web viewWhat Java API says about out object, the same is said with err object also

public class JavaApplication20 {

public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter EMployee ID"); int id=sc.nextInt(); System.out.println("Enter EMployee NAme"); String name=sc.nextLine(); System.out.println("Enter EMployee Age"); int age=sc.nextInt(); System.out.println("Enter EMployee BAsic Salary"); double sal=sc.nextDouble(); double da=.68*sal; double hra=.22*sal; double pf=.12*sal; double netsal=sal+da+hra-pf; System.out.println("EMployee "+id +" NET Salary ==="+netsal); // Repeat the above code for 2 more employees } }

Page 11: 1) Java final variable -    Web viewWhat Java API says about out object, the same is said with err object also

2.c) abstract class Vehicle {double maxSpeed;Vehicle(double s){maxSpeed=s;

} abstract void accelerate(); }

class Truck extends Vehicle { public Truck() { maxSpeed = 80; } public void accelerate{ System.out.println("Truck max speed: " + maxSpeed + " km/h"); } }

public class AbstractClassExample { public static void main(String[] args) { Vehicle t = new Truck();

Page 12: 1) Java final variable -    Web viewWhat Java API says about out object, the same is said with err object also

t.showMaxSpeed();

t.accelerate(); }}

3.a)class Person{String name; Person(String n){ name=n;}public void display1(){ System.out.println(name);}}

class Student extends Person{ int rollno;

Student(String n,int r){ super(n); rollno=r;}public void display2()

Page 13: 1) Java final variable -    Web viewWhat Java API says about out object, the same is said with err object also

{ System.out.println(name+ " "+ rollno); }}class Teacher extends Person{ double sal; String subject;

Teacher(String n,double s,String s1){ super(n); sal=s; subject=s1; } @Override public void display1(){ System.out.println(name+ " "+ sal+" "+subject); }}class CollegeStudent extends Student{ int year; String specialization; CollegeStudent(String n, int r,int y,String s) { super(n,r); year=y;

Page 14: 1) Java final variable -    Web viewWhat Java API says about out object, the same is said with err object also

specialization=s; } @Override public void display2(){ System.out.println(name+ " "+ rollno+" "+year +" "+" "+specialization); } }

public class JavaApplication25 { public static void main(String[] args) { Person p=new Person("Suresh"); p.display1(); Student s=new Student("lenin",1300301); s.display2(); Teacher t=new Teacher("Harish",65000,"Web Technologies"); t.display1(); CollegeStudent cs=new CollegeStudent("VEnkat",1300789,3,"cloud computing"); cs.display2(); } }3)b) Exception:Anexception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program's instructions. When an error occurs within a method, the method creates an object and hands it off to the runtime system. class Example1{ public static void main(String args[]){ try{ System.out.println("First statement of try block"); int num=45/3;

Page 15: 1) Java final variable -    Web viewWhat Java API says about out object, the same is said with err object also

System.out.println(num); } catch(ArrayIndexOutOfBoundsException e){ System.out.println("ArrayIndexOutOfBoundsException"); } finally{ System.out.println("finally block"); } System.out.println("Out of try-catch-finally block"); }}

3)c)Thefinal keywordin java is used to restrict the user. The java final keyword can be used in many context. Final can be:

1.variable2.method3.class

The final keyword can be applied with the variables, a final variable that have no value it is called blank final variable or uninitialized final variable. It can be initialized in the constructor only. The blank final variable can be static also which will be initialized in the static block only. We will have detailed learning of these. 1) Java final variableIf you make any variable as final, you cannot change the value of final variable(It will be constant).Example of final variableThere is a final variable speedlimit, we are going to change the value of this variable, but It can't be changed because final variable once assigned a value can never be changed.

1. classBike9{2. finalintspeedlimit=90;//finalvariable3. voidrun(){4. speedlimit=400;5. }6. publicstaticvoidmain(Stringargs[]){7. Bike9obj=newBike9();8. obj.run();9. }10. }//endofclass

Output:Compile Time Error

Page 16: 1) Java final variable -    Web viewWhat Java API says about out object, the same is said with err object also

2) Java final methodIf you make any method as final, you cannot override it.Example of final method

1. classBike{2. finalvoidrun(){System.out.println("running");}3. }4.5. classHondaextendsBike{6. voidrun(){System.out.println("runningsafelywith100kmph");}7.8. publicstaticvoidmain(Stringargs[]){9. Hondahonda=newHonda();10. honda.run();11. }12. }

Output:Compile Time Error

3) Java final classIf you make any class as final, you cannot extend it.Example of final class

1. finalclassBike{}2.3. classHonda1extendsBike{4. voidrun(){System.out.println("runningsafelywith100kmph");}5.6. publicstaticvoidmain(Stringargs[]){7. Honda1honda=newHonda();8. honda.run();9. }10. }Output:Compile Time Error

------------------------------------------------------------------4.a) class Invoice{ private String partno; private String desc; private int qty; private double price;

Page 17: 1) Java final variable -    Web viewWhat Java API says about out object, the same is said with err object also

Invoice(String p1,String s,int q,double p){ partno=p1; desc=s; qty=q; price=p; } public String getPartno() { return partno; } public String getDesc() { return desc; } public int getQty() { return qty; } public double getPrice() { return price; } public void setPartno(String partno) { this.partno = partno; } public void setDesc(String desc) { this.desc = desc; } public void setQty(int qty) { this.qty = qty; }

Page 18: 1) Java final variable -    Web viewWhat Java API says about out object, the same is said with err object also

public void setPrice(double price) { this.price = price; }public double getInvoiceAmount(){ if(qty<0) qty=0; if(price<0) price=0.0; return qty*price; }}public class JavaApplication24 { public static void main(String[] args){Invoice i=new Invoice("1001","Part1",2,890);System.out.append("Invoice Amount =="+i.getInvoiceAmount());Invoice i1=new Invoice("1002","Part2",-6,200);System.out.append("Invoice Amount =="+i.getInvoiceAmount());Invoice i3=new Invoice("1003","Part3",6,-98);System.out.append("Invoice Amount =="+i.getInvoiceAmount()); } }4.b)package javaapplication24;import java.io.*;public class JavaApplication24 { public static void main(String[] args)throws IOException {BufferedReader br=new BufferedReader(new InputStreamReader(System.in));System.out.println("Enter BAnk Account No");

Page 19: 1) Java final variable -    Web viewWhat Java API says about out object, the same is said with err object also

int accno=Integer.parseInt(br.readLine());System.out.println("Enter BAnk Account Holder NAme");String name=br.readLine();System.out.println("Enter BAnk Account Holder Age");int age=Integer.parseInt(br.readLine());System.out.println("Enter Type of Account (Savings/Current)");String type=br.readLine();System.out.println("Enter BAlance");double bal=Double.parseDouble(br.readLine());System.out.println(accno+" "+name+" "+age+" "+type+" "+bal);System.out.println("Enter Amount to Depoist ");double amt=Double.parseDouble(br.readLine());bal+=amt;System.out.println("Current BALANCE after DEPOSIT "+bal);System.out.println("Enter Amount to WITHDRAW ");double amt1=Double.parseDouble(br.readLine());bal-=amt1;System.out.println("Current BALANCE after WITHDRAW "+bal); } }4.c)interface car { int speed=90; public void distance(); } interface bus { int distance=100; public void speed(); } class vehicle implements car,bus { public void distance() { int distance=speed*100;

Page 20: 1) Java final variable -    Web viewWhat Java API says about out object, the same is said with err object also

System.out.println("distance travelled is"+distance); } public void speed() { int speed=distance/100; } } class maindemo { public static void main(String args[]) { System.out.println("Vehicle"); Vechicle v1=new Vehicle(); v1.distance(); v1.speed(); } }