Department of Computer Science & Engineering Java

6 downloads 0 Views 1MB Size Report
Java : The complete Reference by Herbertz Schildt, Oracle Press. 2. Java: Black Book by Steven Holzner. 3. Java: How to Program by Deitel and Deitel ...
Lab File: Java Programming lab (6CS7A), Dept. of CSE 2017-2018

Department of Computer Science & Engineering

Java Programming Lab Manual By: Asif Munir (Assistant Prof. CSE) Year: 3rd Yr. /6th SEM

Lab Code: -6CS7A

Modern Institute of Technology & Research Centre, Alwar, Rajasthan

Affiliated to Rajasthan Technical University, Kota

1

Lab File: Java Programming lab (6CS7A), Dept. of CSE 2017-2018

Session: 2017-18(Even Sem.) COURSE: B.TECH

YEAR/SEM: 3rd / 6th

NAME OF FACULTY: Asif Munir

Branch: CSE

INDEX Sr. No.

Experiment Number

Page Number

1

Experiment No 1

3 -4

2

Experiment No 2

5-6

3

Experiment No 3

7-9

4

Experiment No 4

10-13

5

Experiment No 5

14-17

6

Experiment No 6

18-21

7

Experiment No 7

22-24

8

Experiment No 8

25-30

9

Experiment No 9

31-33

10

Experiment No 10

34-37

2

Lab File: Java Programming lab (6CS7A), Dept. of CSE 2017-2018

Experiment No. 1 Objective: - Write a program to check whether a number is Armstrong or not Technology: Windows OS, Java Development Kit, and Notepad Theory & Concept An Armstrong of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself. The various examples of Armstrong numbers are 153,370,371,407. Source Code /* Java Program to check whether a three digit number is Armstrong or not*/ import java.util.Scanner; class armstrong { public static void main(String args[]) { int number=0,sum=0,rem=0,cube=0,temp=0; Scanner scan=new Scanner(System.in); System.out.println("Enter a number: "); number=scan.nextInt(); System.out.println("The number entered is:"+" " +number ); temp=number; while(number!=0) { rem=number%10; cube=(int)Math.pow(rem,3); sum=sum+cube; number=number/10; } if(sum==temp) System.out.println(temp+" "+"is an armstrong number"); else System.out.println(temp+" "+"is not armstrong number"); } } 3

Lab File: Java Programming lab (6CS7A), Dept. of CSE 2017-2018

OUTPUT:

CONCLUSIONS:

So after doing the above study we have understood how to create a class and a main function in java. In addition we learned the logic of Armstrong number.

Viva questions: 1. What does static keyword mean? 2. What is an Armstrong number? 3. What is the use of import keyword 4. How is java different from C++ 5. What is the use of scan keyword? References: 1. Java : The complete Reference by Herbertz Schildt, Oracle Press 2. Java: Black Book by Steven Holzner 3. Java: How to Program by Deitel and Deitel

4

Lab File: Java Programming lab (6CS7A), Dept. of CSE 2017-2018

Experiment No. 2 Objective:- Write a program to show the concept of Constructors. Technology: Windows OS, Java Development Kit, and Notepad Theory & Concept Java supports a special type of method called constructor that enables an object to initialize itself when it is created. Constructors have the same name as the class itself. Constructors do not specify a return type, not even void. This is because they return the instance of class itself. Source Code /* Java Program computing the area of rectangle illustrating constructors */ class rectangle { int length, width; rectangle(int x, int y) { length=x; width=y; } void getArea() { int area=length*width; System.out.println("The area of rectangle is: "+ area + " units"); } } class constructorExample { public static void main(String args[]) { rectangle r1=new rectangle(10,20); r1.getArea(); } } OUTPUT: 5

Lab File: Java Programming lab (6CS7A), Dept. of CSE 2017-2018

CONCLUSION:

So after doing the above study we have understood the concept of constructors, how to create constructor and how parameters are passed to the constructor.

Viva questions: 1. What is a constructor? 2. What is the return type of constructor? 3. What are various types of constructors? 4. What is the use of constructor?

References: 1. Java : The complete Reference by Herbertz Schildt, Oracle Press 2. Java: Black Book by Steven Holzner 3. Java: How to Program by Deitel and Deitel 6

Lab File: Java Programming lab (6CS7A), Dept. of CSE 2017-2018

Experiment No. 3 Objective:- Write a program to show the concept of method overloading. Technology: Windows OS, Java Development Kit, and Notepad Theory & Concept In java it is possible to create methods that have same name but different parameters lists and different definitions. This is called method overloading. Method overloading is used when objects are required to perform similar tasks but using different input parameters. When we call a method in an object, Java matches up the method name first, and then the number and type of parameters to decide which one of the definitions to execute. This process is known as polymorphism.

Source Code

/* Java Program computing the area of room illustrating the concept of method overloading*/ class Room { int length; int breadth; Room (int x, int y) { length=x; breadth=y; } Room(int x) { length=x; breadth=x; } void area() { int x=length*breadth; 7

Lab File: Java Programming lab (6CS7A), Dept. of CSE 2017-2018 System.out.println("The area is: "+ x); } } class overloadingExample { public static void main(String args[]) { Room r1=new Room(10,20); r1.area(); Room r2=new Room(30); r2.area(); } } OUTPUT:

CONCLUSION:

So after doing the above study we have understood the concept of method overloading

Viva questions: 1. What is method overloading? 2. What is polymorphism? 8

Lab File: Java Programming lab (6CS7A), Dept. of CSE 2017-2018

3. What is the use of method overloading? References: 1. Java : The complete Reference by Herbertz Schildt, Oracle Press 2. Java: Black Book by Steven Holzner 3. Java: How to Program by Deitel and Deitel

9

Lab File: Java Programming lab (6CS7A), Dept. of CSE 2017-2018

Experiment No. 4 Objective:- Write a program to show the concept of Inheritance. Technology: Windows OS, Java Development Kit, and Notepad Theory & Concept Reusability is another aspect of OOP paradigm. Java supports this concept. Java classes can be reused in several ways. This is basically done by creating new classes, reusing the properties of existing ones. The mechanism of deriving a new class from an old one is called inheritance. The old class is known as base class or super class or parent class and the new one is called the subclass or derived class or child class. The “extends” keyword is used for inheritance. The inheritance allows the subclasses to inherit all the variables and methods of their parent classes. Inheritance may take different forms:  Single Inheritance (Only one super class)  Multiple Inheritances (Several super classes)  Hierarchical Inheritance (One super class, many subclasses)  Multilevel Inheritance (Derived from a derived class) Java does not directly implement multiple inheritances; however this concept is implemented through interface. The diagrammatic representation of various inheritances is shown below:

10

Lab File: Java Programming lab (6CS7A), Dept. of CSE 2017-2018

Fig 4.1: Forms of Inheritance Source Code /* Java Program computing the area of room illustrating the concept of single Inheritance*/ class Room { int length; int breadth; Room(int x, int y) { length=x; breadth=y; } void area() { int z=length*breadth; System.out.println("The area is: "+z); } } class BedRoom extends Room { int height; BedRoom(int a, int b, int c) 11

Lab File: Java Programming lab (6CS7A), Dept. of CSE 2017-2018 { super(a,b); //passes the value to superclass height=c; } void volume() { int v=length*breadth*height; System.out.println("The volume is : "+v); } } class inheritenceExample { public static void main(String args[]) { BedRoom br=new BedRoom(10,20,30); br.area(); br.volume(); } } OUTPUT:

CONCLUSION:

So after doing the above study we have understood the concept of Inheritance. Viva questions: 1. What is an Inheritance? 12

Lab File: Java Programming lab (6CS7A), Dept. of CSE 2017-2018

2. What is the use of inheritance? 3. What are various types of inheritance?

References: 1. Java : The complete Reference by Herbertz Schildt, Oracle Press 2. Java: Black Book by Steven Holzner 3. Java: How to Program by Deitel and Deitel

13

Lab File: Java Programming lab (6CS7A), Dept. of CSE 2017-2018

Experiment No. 5 Objective:- Write a program to show various string operations. Technology: Windows OS, Java Development Kit, and Notepad Theory & Concept Strings represent a sequence of characters. In Java strings are class objects and implemented using two classes namely String and StringBuffer. A Java string is an instantiated object of the String class. Java strings are more reliable and predictable as compared to C strings. A java character is not a character array and is not Null terminated. The most commonly used String Methods are: s2=s1.toLowerCase; //Converts String s1 to lowercase. s2=s1.toUpperCase; //Converts String s1 to uppercase. s2=s1.replace(‘x’,’y’); //Replaces x with y. s1.equals(s2) //Returns true if s1=s2. s1.equalsIgnoreCase(s2) //Returns true if s1=s2, ignoring case of character. S1.length() //Gives length of S1 S1.charAt(n) //Gives nth character of S1 S1.compareTo(s2) //Returns –ve if s1s2, zero if s1=s2 S1.concat(s2) //concatenates s1 and s2 Source Code /* Java Program to show various string operations*/ class stringExample { public static void main(String args[]) { StringBuffer str=new StringBuffer("Object Oriented Language"); System.out.println("Original String: "+str); //obtaining string length System.out.println("The length of string is "+ str.length()); 14

Lab File: Java Programming lab (6CS7A), Dept. of CSE 2017-2018

//Modifying Charecters str.setCharAt(6,'-'); System.out.println("updated string is: "+str); //Appending a string at the end. str.append(" improves security."); System.out.println("string after append is: "+str); //accessing charecters in a string for(int i=0;i

Suggest Documents