Sunteți pe pagina 1din 19

AN ASSIGNMENT IN JAVA

INDEX
QUESTION No.
1

QUESTIONS
WAP to find the largest value among three numbers, using nesting if-else statement in java? WAP to find the area of the rectangle and circle through contructor? WAP for multiple inheritance through interface?

PAGE No.
2 to 3

4 to 5

3 What is method overloading ? WAP for method overloading in JAVA? WAP to sorting a list of numbers using array ? 5 WAP to define the concept of overriding using methods in java? WAP for students records using inheritance ? 7 WAP exception handling using try and catch ? 8 What is package ? How can we define the package? Describe with example. WAP to draw house through APPLET ?

6 to 7

8 to 9

10 to 11

12 to 13

14 to 15 16 to 17

18 to 22

10

23 to 24

1|Page

AN ASSIGNMENT IN JAVA

Q (1). WAP to find the largest value among three numbers, using nesting if-else statement in java? Ans:class abc { int a, b, c; abc(int x, int y, int z) { a = x; b=y c = z; } void cal( ) { if(a > b) { if(a > c) { System . out . println("A is largest") System . out . println("Largest value="+a); } else { System . out . println("C is largest"); System . out . println("Largest value="+c); } } else { if(b > c) { System . out . println("B is largest"); System . out . println("Largest value="+b); }

2|Page

AN ASSIGNMENT IN JAVA

else { System . out . println("C is largest"); System . out . println("Largest value="+c); } } } } class ifelse { public static void main(String args[ ]) { abc T = new abc(14,16,5); T . cal( ); } }

OUTPUT:-

Q (2). WAP to find the area of the rectangle and circle through contructor? Ans:class abc { int l, b;
3|Page

AN ASSIGNMENT IN JAVA

float r; abc(int x, int y, float z) { l = x; b = y; r = z; } void rect( ) { int a = l * b; System . out . println("Area of Rectangle="+a); } void circle( ) { float a = (float)3.14 * r * r; System . out . println("Area of Circle="+a); } } class area { public static void main(String args[ ]) { abc T = new abc(5, 5, 5); T . rect( ); T . circle( ); } }

OUTPUT:

4|Page

AN ASSIGNMENT IN JAVA

Q (3). WAP for multiple inheritance through interface? Ans:interface A { int a = 2; } interface B { int b = 6; } class abc implements A, B { void show( ) { System . out . println("A="+a); System . out . println("B="+b); } } class multiple { public static void main(String [ ] args) { abc T = new abc( ); T . show( ); } }

5|Page

AN ASSIGNMENT IN JAVA

OUTPUT:-

Q (4). What is method overloading ? WAP for method overloading in JAVA? Ans:class abc { int l, b; float r; void area(int x, int y) { l = x; b = y; int a = l * b; System . out . println("Area of Rectangle="+a); } void area(float b) { r = b; float a = (float)3.14 * r * r; System . out . println("Area of Circle="+a); } } class method {
6|Page

AN ASSIGNMENT IN JAVA

public static void main(String args[ ]) { abc T = new abc( ); T . area(5, 5); T . area(5); } }

OUTPUT:-

Q (5). WAP to sorting a list of numbers using array ? Ans:class abc { int t, len; int a[ ]={9,35,28,49,55,23,90,23,33}; abc( ) { t = 0; len = a . length; } void show( )
7|Page

AN ASSIGNMENT IN JAVA

{ for(int i=0; i<len; i++) { System . out . print(+a[i]); System . out . print(" "); } System . out . println(" "); } void cal( ) { for(int i=0; i<len; i++) { for(int j=i+1; j<len; j++) { if(a[i]> a[j]) { t = a[i]; a[i]=a[j]; a[j]=t; } } } } } class sort { public static void main(String args[ ]) { abc T = new abc( ); System . out . println("Array list"); T . show( ); T . cal( ); System . out . println("After sorting array list"); T.show( ); } }
8|Page

AN ASSIGNMENT IN JAVA

OUTPUT:-

Q (6). WAP to define the concept of overriding using methods in java? Ans:class abc { void show( ) { System . out . println("METHOD OF SUPER CLASS"); } } class xyz extends abc { void show( ) { System . out . println("METHOD OF SUB CLASS"); } } class overriding { public static void main(String [ ] args) {
9|Page

AN ASSIGNMENT IN JAVA

xyz T = new xyz( ); T . show( ); } }

OUTPUT:-

Q (7). WAP for students records using inheritance ? Ans:class abc { String a[ ]={"ANKIT ", "AMIT ", "MANJIT"}; int b[ ]={1,2,3}; String c[ ]={"BCA"}; } class xyz extends abc { void show( ) { for(int i=0; i<3; i++) {
10 | P a g e

AN ASSIGNMENT IN JAVA

System . out . print("Roll="+b[i]); System . out . print(" "); System . out . print("Name="+a[i]); System . out . print(" "); System . out . println("Class="+c[0]); System . out . println(" "); } } } class record { public static void main(String args[ ]) { xyz T = new xyz( ); System . out . println("RECORDS OF STUDENTS"); T . show( ); } }

OUTPUT:-

11 | P a g e

AN ASSIGNMENT IN JAVA

Q (8). WAP exception handling using try and catch ? Ans:class abc { int a; int b, c; abc(int x) { a = 5; b = x; } void show( ) { try { c = a/b; System . out . println(+c); } catch(ArithmeticException A) { System . out . println("DIVISION BY ZERO IN ARITHMETIC EXPRESSION"); } } } class except { public static void main(String args[ ]) { abc T= new abc(0); T . show( ); } }
12 | P a g e

AN ASSIGNMENT IN JAVA

OUTPUT:-

Q (9). What is package ? How can we define the package? Describe with example. Ans:PACKAGE:Packages are contain a set of classes in order to ensure that class names are unique. Packages are stored in a hierarachical manner and are explicitly imported into new class definition. The main features of OOPs is its ability to reuse the code already created. The inheritance is limited to reusing the classes within a program. If we need to use classes from other programs without physically copying them into the program under development, this can be accomplished in java by using packages. Packages contain a set of classes and interface in order to ensure that names are unique. Packages are containers for classes that are used to compartmentalize the class space. By organising our classes into packages we achieve the following benefits: a). The classes contained in the packages of other programs can be
13 | P a g e

AN ASSIGNMENT IN JAVA

easily reused. b). In packages, classes can be unique compared with classes in other packages. That is, two classes in two different packages can have the same name. They may be referred by their fully qualified name, comprising the package name and the class name. c). Packages provide a way to hide classes thus preventing other programs or packages from accessing classes that are meant for internal use only. d). Packages also provide a way for separating design from coding. A period is used as separator to enable this.

e.g. packages < base packages > . < my packages > . < address >

Creation of a package:The following steps are involved in the creation of a package: Step(1). Create a directory, which has the same name as the package. Step(2). Include package command, along with the package name, as the first statement in the program. Step(3). Write class declarations. Step(4). Save the file in this directory as Class_name.java, where the class_name is the name of the class. Step(5). Compile this field using javac. Step(6). There are two ways of using this program change to previous level directory and use java package name. class name or set the CLASSPATH variable accordingly. e.g. (i) Create a directory called pack. (ii) Open a new and enter the following code:
14 | P a g e

AN ASSIGNMENT IN JAVA

package pack; public class class1 { Public static void great( ) { System . out . println(Hello); } } Now save this file as class1.java inside the pack directory. Using package the program:To make the use of classes within different packages, the import statement is used. Once imported, the classes within the package can be used the program. e.g. (i) Open new file and enter the following code: import pack .*; class importer { public static void main(String [ ] args) { class1.great( ); } } (ii) Save the file in the root directory of the packages and compile and run it. Java API provides a large number of classes grouped into different packages according of functionality. This packages is called API package, built-in packages or system packages.

15 | P a g e

AN ASSIGNMENT IN JAVA

Java system packages are following-

Java . lang:- Language supports classes. The java.lang package provides the classes and interfaces the form the core of the java language and the java virtual machine. They include classes for primitive types, string with functions, threads and exceptions.

Java . util:- This package contains a collection of utility classes and related interfaces. It includes classes that provide generic data structure (Hashtable, Stack, Vectors), String manipulation(string Tokenizer), calendar and data utilities (DATE). It also contains observer interface and observer class. Java . io:Input/output support classes. They provide a set of I/O streams used to read and write data to files or other I/O sources.

Java . awt:- The abstract window tool kit package. This packages provides the standard graphical user interface elements such as buttons, list, menus text areas and so on. Java . net:- The java networking package. This package contains networking classes and interfaces, including classes that represent a URL and URL connection, classes that implements a socket connection, and a class that represents an internet address. Java . applet:- This packages contains classes and interfaces for creating applets. e.g.
16 | P a g e

AN ASSIGNMENT IN JAVA

import java . lang . *; class abc { void show(int x, int y) { int area = x * y; System . out . println("AREA="+area); } }

class imp { public static void main(String [ ] args) { abc T= new abc( ); System . out . println("IMPORTING PACKAGE = Java . lang . *"); T . show(3,5); } }

OUTPUT:-

17 | P a g e

AN ASSIGNMENT IN JAVA

Q (10). WAP to draw house through APPLET ? Ans:import java . awt .*; import java . applet . *; public class house extends Applet { public void paint(Graphics g) { g . drawLine(100,30,250,30); int a[ ]={50,150,100}; int b[ ]={100,100, 30}; int c = a . length; g . drawPolygon(a, b, c); g . drawOval(80,50,40,40); g . drawLine(250,30,300,100); g . drawLine(150,100,300,100); g . drawLine(300,100,300,200); g . drawLine(150,200,300,200); g . drawLine(150,100,150,200); g . drawLine(80,140,120,140); g . drawLine(120,140,120,200); g . drawLine(80,140,80,200); g . drawLine(50,100,50,200); g . drawLine(50,200,150,200); } }

HTML code for applet viewer:<html> <Applet code = house . class width = 250 height = 200> </applet> </html>

18 | P a g e

AN ASSIGNMENT IN JAVA

OUTPUT:-

19 | P a g e

S-ar putea să vă placă și