Harish Java Homework
Harish Java Homework
Ques 1: What are nested classes? Differentiate between static and non-static nested class. WAP to demonstrate nested classes in which there are two classes, one is DemoNested and other is Nested. In class Nested, create two nested classes one static and other nonstatic. Now from DemoNested class access the static/non-static private data members and methods of nested classes and explain the difference between both of them. Ans:- One of the highly advantageous features of Java is the ability to define nested classes, i.e. classes within classes. In Java classes are either top-level-classes (classed defined directly within a package) or nested classes (classes defined within top-levelclasses).In brief it can be said that nested class is nothing but a class that is declared entirely within the body of a class or an interface. In Java nested classes can be static or non static. Non static nested classes are defined within the top-level-classes and they are bound by the scope of their enclosing class. Non-static nested classes can have direct access to all the members (even the private ones) of their enclosing class. It is also worth to mention that nested classes (in general) can be defined from within any block inside a top-levelclass, that could be a method block or even a control statement target block (such as if, for or while). On the other hand static nested classes exist in isolation from the top-level-class that they are defined within. On the contrary to the non-static nested classes, static nested classes cant directly access the members of the class that they are defined in (simply because they are static members, and in static class,members can only access other static members directly), but they can do so using an object instance of the enclosing toplevel-class. Public class nested //main nested class { private int private_member_variable=100; // static nested class public static class static_inner { public void print_private_variable() {
nested outerclass=new outerclass(); system.out.println(nested.private_member_variable); } } //non static nested class public class non_static_inner { private void print_private_variable() { system.out.println(private_member_variable); } public void call_inner_method() { non_static_inner innerclass=new innerclass(); innerclass.print_private_variable(); }
}
} public class demonested //demonested class { public static void main(String args[]) { nested n=new nested(); nested.static_inner s=new nested.static_inner; static_inner.print_private_variable; nested.non_static_inner=new non_static_inner; non_static_inner.print_private_variable; } Ques 2:-Define method overloading and method overriding. What is the difference between both of them? WAP to demonstrate method overloading and method overriding (There should be a super class named Parent and a sub-class named as Child. Overload the display method of Parent class, and also override the same in Child class. ). Ans :- With the Java language, you can overload methods. Overloading is the practice of supplying more than one definition for a given method name in the same clss. However several restrictions govern an acceptable set of overloaded methods: Any two methods in a set of overloaded functions must have different argument lists
A difference in return type only is not sufficient to constitute an overload and is illegal. Method overriding allows a sub class to provide its own implementation of a method already provided by one of its super classes. A sub class inherits methods from a super class. Sometimes, the sub class may want to provide a new version of methods defined in the super class. This is referred to as method overriding. Several restrictions govern any particular method to override another correctly: The return type and method signature must be identical to those of a method in the super class. The accessibility must not be more restrictive than original method. The overriding method must not throw checked exceptions of classes that are not possible for the original method. Following are the points that clearly differentiate overloading for overriding of methods:1)First and most important difference between method overloading and overriding is that, In case of method overloading in Java, Signature of method changes while in case of method
overriding it remain same.
2) Second major difference is that one can overload method in one class but overriding can only be done on subclass. 3) You can not override static, final and private method in Java but you can overload static, final or private method in Java. 4) Overloaded method in Java is bonded by static binding and overridden methods are subject to dynamic binding. 5) Private and final method can also be not overridden in Java. Public class parent { void display(int a,int b) {} } public class child extends parent { void display(int a,int b) //method overrriding {
system.out.println(a+b); } void display(string a) //method overloading { system.out,println(a); } void display(int a, int b,int c) //method overloading { system.out.println(a+b+c); } void display( char d, char e) //method overriding { system.out.println(d+e); } } public static void main(String args[]) { parent p= new p; p.display(5,8); p.display(harish); p.display(2,8,7); p.display('h','s'); } Ques 3: Why String class is immutable? Write a program that prompts the user to enter his name, and then computes and the initials from users full name and displays. Ans:- String class is immutable because by doing so it can safely be shared between many threads, by which we avoid synchronization issues. We can store string in string pool for .If it was mutable then this would not be possible .String immutability allows to cache its hash value which greatly improves String performance in Hash based collections. A String class is immutable that means there is no way to change its contents (the state of the object) once it is created. There are no methods provided that allow you to manipulate the contents of the string once it is created. Methods provided with the string class that operate on a string always return a new string rather than returning an updated copy of the old one. Moreover the immutability of string class has been done as several security features of the Java programming language depend upon String objects .For conserving memory by reusing the existing String object, the string class is made immutable. import java.util.Scanner;
public class hs { public static void main(String args[]) { Scanner s=new Scanner(System.in); System.out.println("Enter name"); String on=s.nextLine(); System.out.println("The initials of the name are"); System.out.println(on.charAt(0)); System.out.println(on.charAt(6)); } } Ques 4: What is Typecasting? What is the need of typecasting? Explain in your words. WAP that prompts the user to enter the month, year and first day of the year, and displays the calendar for the entered month of that year. Ans:- In Java object typecasting,one object reference can be type cast into another object reference. The cast can be to its own class type or to one of its subclass or superclass types or interfaces. Typecasting can be thus upcast or downcast. In short Type casting is used to convert the data type from lower data type to higher data type and vice versa in order to perform operations with different data types in an expression. Consider the Syntax: Type variable1= (type)variable2; The importance of type conversion can be understood by its types.It is needed to convert from one type to another during program compilation type. Types of casting: 1. Implicit typecasting: Implicit typecasting is that typecasting which occurs automatically by the compiler when data type is converted into the higher type from lower. Eg: short x=500; Int y; y=x; 2. Explicit typecasting: Explicit typecasting means that typecasting which does not occur automatically rather when they occur when programmer himself wants to convert a higher data type to lower type or vice-versa. Eg: short x=500;
int y; y = (int) x; import java.io.Calendar; class calendar { public static void main(String args[])throws IOException { int i,j,k,m,p=0,r,q,yy,i1,g; string ss; DataInputStream x=new DataInputStream(System.in); //making a string array for months string s[]={"","January","February","March","April","May","June","July","August","Septemb er","October","November","December"}; // string array for weekdays string str[]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"}; //array for monthdays int b[]={0,31,28,31,30,31,30,31,31,30,31,30,31}; System.out.println("ENTER THE FIRST DAY OF THE YEAR,YEAR AND MONTH"); ss=x.readLine(); yy=Integer.parseInt(x.readLine()); m=Integer.parseInt(x.readLine()); if(yy%4==0) //Leap year { b[2]=29; } int ar[][]=new int[6][7]; for(i=1;i<=m;i++) { System.out.println("\t\t"+s[i]); for(i1=0;i1<6;i1++) { for(j=0;j<7;j++) { ar[i1][j]=0; }} for(j=0;j<7;j++)
ar[0][k]=r; r=r+1; } for(k=1;k<6;k++) { for(j=0;j<7;j++) { ar[k][j]=r; r=r+1; if(r>b[i]) { break; } } if(r>b[i]) { break; } } for(k=0;k<6;k++) { for(j=0;j<7;j++) { if(ar[k][j]==0) { System.out.print(" \t } else {
");
System.out.print(ar[k][j]+" \t "); } } System.out.println(); } q=b[i]%7+p; if(q<7) { p=q; } else { p=q-7; } } } } Ques 5: What is the need of interfaces? How it is different from abstract class? WAP to demonstrate inheritance. Define a class Invoke which inherits a class DemoIn and implements an interface Showable. There are at least two methods in Showable. Define another class with main() and create the objects of both classes Invoke and DemoIn and invoke the methods of both the classes. Ans:-An interface is a collection of abstract methods. A class implements an interface, thereby inheriting the abstract methods of the interface. An interface is not a class. Writing an interface is similar to writing a class, but they are two different concepts. A class describes the attributes and behaviors of an object. An interface contains behaviors that a class implements. Main purpose of interface is to provide polymorphic behavior. Just it is used to define some general information which can be used for specific purpose as we can define an interface with some attributes and method.
{ i=10; j=20; } void display() { System.out.println("The value of i is"+i); System.out.println("The value of j is"+j); } }
interface Showable
{ int z=30; int area(int x,int y); int vol(int x,int y,int z); } class Invoke extends DemoIn implements Showable { public int area(int x,int y) { int area1; area1=x*y; return area1; } public int vol(int x,int y,int z) { int vol1; vol1=x*y*z; return vol1; }
} public class NEW { public static void main(String args[]) { DemoIn obj1=new DemoIn(); obj1.display(); Invoke obj2=new Invoke(); int s=obj2.area(2, 3); int v=obj2.vol(2, 3, 4); System.out.println("The area is"+s); System.out.println("The volume is"+v); } } Ques 6: WAP to create a 44 two-dimensional array and randomly fills in 0s and 1s into it. Display the filled array and find the rows and columns with all 0s or all 1s. Ans:import java.io.DataInputStream; class NewClass { public static void main(String args[]) { int b=0,c=0; double[][] a=new double[4][4]; try { System.out.println("Enter random digit 0 or 1 in the matrix"); for(int i=0;i<4;i++)
{ for(int j=0;j<4;j++) { DataInputStream obj1=new DataInputStream(System.in); a[i][j]=Integer.parseInt(obj1.readLine()); } System.out.println(); } System.out.println("This is a (4*4) two-dimensional matrix"); for(int i=0;i<4;i++) { for(int j=0;j<4;j++) { System.out.print(a[i][j]+ " "); } System.out.println(); }
if(a[0][0]==0 && a[1][0]==0) { if(a[2][0]==0 && a[3][0]==0) b++; } else if(a[0][0]==1 && a[1][0]==1) { if(a[2][0]==1 && a[3][0]==1) c++; }
if(a[0][0]==0 && a[0][1]==0) { if(a[0][2]==0 && a[0][3]==0) b++; } else if(a[0][0]==1 && a[0][1]==1) { if(a[0][2]==1 && a[0][3]==1) c++; } System.out.println("The no. of rows and columns with all O's are "+b); System.out.println("The no. of rows and columns with all 1's are "+c);