MSBTE Solution App-1 PDF
MSBTE Solution App-1 PDF
MSBTE Solution App-1 PDF
jdk1.8.0 or above.
1)
< ~Less than
> ~Greater than
<= ~Less than or equal to
>= ~Greater than or equal to
== ~Equality operator
3) Example :
class Main {
public static void main(String[]
int number = -5;
if (number > 0) {
System.out.println("The number is positive.");
}
else {
System.out.println("The number is not positive.");
}
System.out.println("Statement outside if...else block");
}
}
Number is
less than 100
Number is
greater than
50
Number is
Positive.
This statement is
always executed.
Program :
public class SwitchCase {
public static void main(String args[]){
int num=2;
switch(num+2){
case 1:
System.out.println("Case1: Value is: "+num);
case 2:
System.out.println("Case2: Value is: "+num);
case 3:
System.out.println("Case3: Value is: "+num);
default:
System.out.println("Default: Value is: "+num);
}
}
}
error: duplicate
case label
case 100:
2) Program :
4) Hence, a loop which will never stop or i.e over end is named to be infinite
loop.
It the terminating condition is not given or if the condition cause the loop
to start again then it is called as infinite loop.
1) class check
{
public static void main(string args[])
{
for (i=1; i<100; i++ )
{
if (i%2 ==0)
{
System.out.print (;);
}
}
}
}
Output : 1
2
3
4
5
1)
While do-while
1)While loop first check the condition 1) do-while loop firstly enter in the
then enter in the body. loop then check the condition.
2) While loop is entry controlled loop 2) do-while loop is exit controlled loop.
1
2
3
4
5
6
7
8
9
10
12121212121212
12121212121212
12121212121212
12121212121212
12121212121212
2)Program :
public class Main {
public static void main(String[] args) {
int i = 1, n = 50;
do {
System.out.println(i);
i++;
}
while(i <= n);
}
}
byte value : 12
short value : 12
int value : 12
long value : 12
float value : 12.0
double value : 12.0
1) 1) Byte.
2) Short.
3) Int.
4) Long.
5) Float.
Output :
Error:
possible lossy conversion
from int to char
ch = num;
Output = 1
1) Type casting is the process of converting an entity of one data type into
another data type.
2) Implicit Explicit
3) The narrowing conversion occurs form a data type to a different type that
has smaller size such as form a long (64 bits) to an (32 bits)
In general, narrowing primitive conversion can occur in these case short
to byte or char.
100
result = 50.151999
Program :
public class Explicit
{
public static void main(String[] args)
{
byte x = 10;
long y = (long)x;
double z = (double)y;
double result = x+y+z;
System.out.println(result);
}
}
class Example
{
private int var;
public Example()
{
this.var = 10;
}
public Example(int num)
{
this.var = num;
}
public int getValue()
{
return var;
}
public static void main(String args[])
{
Example obj = new Example();
Example obj2 = new Example(100);
System.out.println("var is: "+obj.getValue());
System.out.println("var is: "+obj2.getValue());
}
}
var is: 10
var is: 100
0 (zero)
3)
public class Complex
{
int Real,Imag;
Complex()
{}
Complex(int Real1,int Imag1)
{
Real=Real1;
Imag=Imag1;
}
Complex AddComplex(Complex C1,Complex C2)
{
Complex CSum=new Complex();
CSum.Real=C1.Real+C2.Real;
CSum.Imag=C1.Imag+C2.Imag;
return CSum;
}
}
class Complexmain
{
public static void main(String[] a)
{
Complex C1=new Complex(4,8);
Complex C2=new Complex(5,7);
Complex C3=new Complex();
C3=C3.AddComplex(C1,C2);
System.out.println("SUM:" + C3.Real +"+i" + C3.Imag);
}
}
Beginnersbook
hello
Java String Example
1. 1) String()
2) String(byte[ ] byte)
3) String (Char [ ] value)
4) String (String original)
4. equal ( ) Compare ( )
Output :
abc
False
True
2. class Str
{
public static void main( String args[] )
{
StringBuffer s = new StringBuffer("Coding Atharva");
System.out.println("\n String = "+s);
System.out.println("\n Length = "+s.length() );
System.out.println("\n Length = "+s.capacity() );
s.setLength(6);
System.out.println("\n After setting length String = "+s );
s.setCharAt(0,'K');
System.out.println("\n SetCharAt String = "+s );
s.setCharAt(0,'C');
int a = 007;
s.append(a);
System.out.println("\n Appended String = "+s );
s.insert(6," Atharva");
System.out.println("\n Inserted String = "+s );
s.reverse();
System.out.println("\n Reverse String = "+s );
s.reverse();
s.delete(6,14);
System.out.println("\n\n After deleting string="+s);
}
}
class Testarray
{
public static void main(String args[])
{
int a[]=new int[5];
a[0]=10;//initialization
a[1]=20;
a[2]=70;
a[3]=40;
a[4]=50;
for(int i=0;i<a.length;i++)
System.out.println(a[i]);
}
}
10
20
70
40
50
4 error
[I@2c7b84de
0
class ForEach
{
public static void main( String args[] )
{
int a[ ]={1,1,2,3,4,5,5};
for(int i: a )
{
System.out.println(i);
}
}
}
Downloaded From Maha360 App
Maha360 App
import java.util.Vector;
class Main {
public static void main(String[] args) {
Vector<String> mammals= new Vector<>();
mammals.add("Dog");
mammals.add("Horse");
mammals.add(2, "Cat");
System.out.println("Vector: " + mammals);
Vector<String> animals = new Vector<>();
animals.add("Crocodile");
animals.addAll(mammals);
System.out.println("New Vector: " + animals);
}
}
Downloaded From Maha360 App
Maha360 App
1. Size ( ) Capacity ( )
2. addElement( ) insertElement ( )
3. Array Vector
20
0
import java.util.Vector;
public class VectFun
{
public static void main( String args[] )
{
Vector v = new Vector();
v.addElement(" Coding with Raman ");
v.add(007);
System.out.println(" Capacity= "+v.capacity() );
System.out.println(" Contains= "+v.contains(007) );
System.out.println(" Element At 0= "+v.elementAt(0) );
System.out.println(" Element At 0= "+v.elementAt(0) );
System.out.println(" Enumeration of Components = "+v.elements() );
System.out.println(" First Element = "+v.firstElement() );
System.out.println(" Last Elements = "+v.lastElement() );
System.out.println(" Index Of = "+v.indexOf(007) );
v.insertElementAt(1,0);
System.out.println(" Elements After Inserting = "+v );
v.removeElementAt(0);
System.out.println(" Elements After Removing = "+v );
System.out.println(" Remove Element = "+v.removeElement(007) );
System.out.println(" Elements After Removing = "+v );
System.out.println(" Size = "+v.size() );
Object obj[] = new Object[10];
v.copyInto(obj);
v.clear();
System.out.println(" Elements After Clearing = "+v );
}
}
30
class MyBaseClass{
protected void disp()
{
System.out.println("Parent class method");
}
}
class MyChildClass extends MyBaseClass{
public void disp(){
System.out.println("Child class method");
}
public static void main( String args[]) {
MyChildClass obj = new MyChildClass();
obj.disp();
}
}
interface A
{
public void dis(int num);
}
interface B
{
public void disp(int num);
}
class Mult implements A, B
{
public void dis(int num)
{
System.out.println("From Interface A number is " + num);
}
public void disp(int num)
{
System.out.println("From Interface B number is " + num);
}}
public class Test extends Mult
{
public static void main(String args[])
{
Mult ml = new Mult();
ml.dis(10);
ml.disp(20);}}
1. class Shape {
public void display() {
System.out.println("Inside display");
}
}
class Rectangle extends Shape {
public void area() {
System.out.println("Inside area");
}
}
class Cube extends Rectangle {
public void volume() {
System.out.println("Inside volume");
}
}
public class Tester {
public static void main(String[] arguments) {
Cube cube = new Cube();
cube.display();
cube.area();
cube.volume();
}
}
2. class Room
{
int length;
int breadth;
Room(int x,int y)
{
length=x;
breadth=y;
}
int area()
{
return(length*breadth);
}}
class BedRoom extends Room
{
int height;
BedRoom(int x,int y,int z)
{
super(x,y);
height=z;
}
int volume()
{
return (length*breadth*height);
}}
class InherTest
{
public static void main(String args[])
{
BedRoom room=new BedRoom(14,12,10);
int area=room.area();
int volume=room.volume();
System.out.println("Area = "+area);
System.out.println("Volume = "+volume);
}}
interface Printable{
void print();
}
interface Showable{
void show();
}
class A7 implements Printable,Showable{
public void print(){System.out.println("Hello");}
public void show(){System.out.println("Welcome");}
public static void main(String args[]){
A7 obj = new A7();
obj.print();
obj.show();
}
}
Hello
Welcome
1. Class Interface
3. Advantages of interfaces :
{
double pi = 3.14;
double calc(double x,double y);
}
package MyPackage;
public class Compare {
int num1, num2;
Compare(int n, int m) {
num1 = n;
num2 = m;
}
public void getmax(){
if ( num1 > num2 ) {
System.out.println("Maximum value of two numbers is " + num1);
}
2. package myInstitute;
public class Department
{
public void display()
{
int sr=1;
String Name="Ajay";
long no=987654321;
String desig="H.O.D";
System.out.println("Details of Staff are");
System.out.println("Srno = "+sr);
System.out.println("Name = "+Name);
System.out.println("Designation = "+desig);
System.out.println("Phone Number = "+no);
}
}
import myInstitute.*;
class JavaApplication
{
public static void main(String args[])
{
Department obj = new Department();
obj.display();
}
}
Thread 11 is running
3. notify notifyAll
class Main
{
public static void main(String[] args)
{
try {
int divideByZero = 5 / 0;
}
catch (ArithmeticException e){
System.out.println("ArithmeticException => " +
e.getMessage());
}
finally {
System.out.println("This is the finally block");
}
}
}
2. error exception
It is irrecoverable It is recoverable
class ThrowExcep
{ static void fun()
{ try
{ throw new NullPointerException("demo");}
catch(NullPointerException e){
System.out.println("Caught inside fun().");
throw e;
}}
public static void main(String args[]){
try {
fun(); }
catch(NullPointerException e) {
System.out.println("Caught in main.");
}}}
1. Throw Throws
3. Hello Java
//First.java
import java.applet.Applet;
import java.awt.Graphics;
public class First extends Applet{
public void paint(Graphics g){
g.drawString("welcome",150,150);
}
}
welcome
1. init( ) start( )
2. A start() method is often used to start any threads the applet will
need while it runs.
The stop() method is called at least once in an applet's life, when
the browser leaves the page in which the applet is embedded
1. package Applet;
import java.applet.*;
import java.awt.*;
}
}
3. import java.awt.*;
import java.applet.*;
4. applets applications
3. setforeground() setcolour ( )
1. import java.awt.*;
import java.applet.*;
public class DrawingPolygons extends Applet
{
public void paint(Graphics g)
{
int x[] = { 70, 150, 190, 80, 100 };
int y[] = { 80, 110, 160, 190, 100 };
g.drawPolygon (x, y, 5);
}
}
2. import java.applet.*;
import java.awt.*;
public class Human_Face extends Applet
{
public void init()
{
setBackground(Color.white);
}
public void paint(Graphics g)
{
Color clr=new Color(255,179,86);
g.setColor(clr);
g.drawOval(100,100,250,300);
g.fillOval(100,100,250,300);
g.setColor(Color.black);
g.drawOval(160,185,40,25);
g.fillOval(160,185,40,25);
g.drawOval(250,185,40,25);
g.fillOval(250,185,40,25);
g.drawArc(160,170,35,10,0,180);
g.drawArc(250,170,35,10,0,180);
g.drawLine(210,265,210,275);
g.drawLine(240,265,240,275);
g.drawArc(210,275,30,10,0,-180);
g.drawArc(175,300,100,50,0,-180);
}
}
import java.applet.*;
import java.awt.*;
public class Geo extends Applet
{
public void paint(Graphics g)
{
g.setColor(Color.GREEN);
g.drawLine(20,20,100,20);
g.drawRect(20,50,90,90);
g.fillRoundRect(130,50,120,70,15,15);
g.setColor(Color.RED);
g.drawOval(20,160,160,100);
g.fillOval(180,160,160,100);
}
}
/* <applet code="Geo.class" width =300
height=300>
</applet>
*/
1. b) paint( )
1. Output :-
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class IOStreamsExample {
public static void main(String args[]) throws IOException {
File file = new File("D:/myFile.txt");
FileInputStream fis = new FileInputStream(file);
byte bytes[] = new byte[(int) file.length()];
fis.read(bytes);
File out = new File("D:/CopyOfmyFile.txt");
FileOutputStream outputStream = new FileOutputStream(out);
outputStream.write(bytes);
outputStream.flush();
System.out.println("Data successfully written in the specified
file");
}
}
1. import java.io.*;
class Copy
{
public static void main(String args[])throws IOException
{
FileInputStream Fread =new FileInputStream("p1.txt");
FileOutputStream Fwrite=new FileOutputStream("p2.txt") ;
System.out.println("File is Copied");
int c;
while((c=Fread.read())!=-1)
Fwrite.write((char)c);
Fread.close();
Fwrite.close();
}}
2. import java.io.*;
public class Byte {
public static void main(String[] args)
{
String strFilePath = "D://p1.txt";
try
{
FileOutputStream fos = new FileOutputStream(strFilePath);
String strContent = " Raman ";
fos.write(strContent.getBytes());
fos.close();
System.out.println("Data is written into the file named as p1");
}
catch(FileNotFoundException ex)
{
System.out.println("FileNotFoundException : " + ex);
}
catch(IOException ioe)
{
System.out.println("IOException : " + ioe);
}}}
3. import java.io.*;
class Copy
{
public static void main(String args[])
{
try
{
FileInputStream fr=new FileInputStream(new File(args[0]));
int i=0;
while((i=fr.read())!=-1)
{
System.out.print((char)i);
}
fr.close();
}
catch(ArrayIndexOutOfBoundsException ex)
{
System.out.println(" C:\\Users\\Desktop\\File1.txt");
}
catch(IOException ex)
{
System.out.println("File Does Not Found in given Directory. ");
}
}
}