Java Assignment Richa
Java Assignment Richa
What is javac,
javap and javadoc.
Installation of jdk :
Go to the Oracle site and open the Java SE download page. Under the latest version
Next, click on the Accept License Agreement button and choose your version of Java for
Windows (32-bit or 64-bit) to proceed with downloading the JDK executable file.
In this step, we will be running the executable JDK file (It will be a file with .exe as
an extension) once the download is done. This installs JDK as well as JRE. For
1
To begin the installation, we need to double-click on the downloaded file, and we
Click on Next to proceed with the installation, and follow the Installation guide
2
To recover some of our system’s disk space, it is good practice to delete the
JDK gets installed in the C directory of our system by default having the path “C:\
We will need to update our system’s Environment variables with our installed JDK
bin path to run the Java programs because while executing the programs, the
command prompt will look for the complete JDK bin path.
3
The PATH variable in our system provides the exact location of executables that
will be used for running Java programs, such as javac and java. The CLASSPATH
If we do not set the PATH variable, we will specify the full path to the JDK bin
Inside Properties, in the left-side panel, select Advanced System Settings, and here
4
Click on New, and type PATH in the Variable Name, and enter the path of the bin
If we already have the PATH variable, we can edit it by adding it to the existing
values.
5
Click on the OK button to apply the changes.
Open the command prompt and enter the command “java –version”, and if it runs
Now that we have seen the steps to install JDK, let the programming fun begin!
6
Javac :
javac is the Java Compiler. It's used to compile Java source code (files with a .java
extension) into bytecode, which can be executed by the Java Virtual Machine (JVM).
Example usage: javac HelloWorld.java will compile the HelloWorld.java source code file
into a HelloWorld.class bytecode file.
Let's assume you have a Java source file named HelloWorld.java with the following
content:
After running this command, if there are no syntax errors in your code, it will generate a
bytecode file named HelloWorld.class, which you can run using the java command
7
This will disassemble the HelloWorld.class file and display the bytecode instructions,
which might look something like this:
Compiled from "HelloWorld.java"
This will generate HTML documentation for the Person class with descriptions of the
class, constructor, and methods. You can view this documentation in a web browser.
8
Q 2. Write a program to count the number of objects of a class.
Source Code :
class CountObj{
static int i;
CountObj()
{
i++;
}
void geti()
{
System.out.println("Number of Object "+i);
}
public static void main(String[] args){
CountObj c1=new CountObj();
CountObj c2=new CountObj();
CountObj c3=new CountObj();
CountObj c4=new CountObj();
c1.geti();
}
}
Output :
9
Q 3. Write a program to assign object reference variable to another and
delete the reference to object and garbage collection.
Source Code :
public class GCTest
{
GCTest()
{
System.out.println("object is created");
}
protected void finalize()
{
System.out.println("object is garbage collected.");
}
public static void main(String[] args)
{
System.out.println("Richa Dubey Roll no.: 23238 MCA-1");
GCTest t1=new GCTest();
GCTest t2=new GCTest();
t1=t2;
System.gc();
}
}
Output :
10
Q 4. Read only one parameter from command line argument and display the
welcome message also check the length.
Source Code :
class CommandLine{
public static void main(String args[]){
System.out.println("Richa Dubey Roll no.: 23238 MCA-1");
System.out.println("Welcome "+args[0]);
System.out.println("length is:"+args[0].length());
}
}
Output :
11
Q 5. Create a class named 'Rectangle' with two data members 'length' and
'breadth' and two methods to print the area and perimeter of the rectangle
respectively. Its constructor having parameters for length and breadth is
used to initialize length and breadth of the rectangle. Let class 'Square'
inherit the 'Rectangle' class with its constructor having a parameter for its
side (suppose s) calling the constructor of its parent class as 'super(s,s)'.
Print the area and perimeter of a rectangle and a square.
Source Code :
class Rectangle{
private double length;
private double breadth;
12
public static void main(String[] args){
Rectangle rectangle=new Rectangle(10,5);
Rectangle square=new Square(10);
System.out.println("Richa Dubey Roll no.: 23238 MCA-1");
System.out.println("Area and perimeter for rectangle:");
rectangle.area();
rectangle.perimeter();
System.out.println("Area and perimeter for square:");
square.area();
square.perimeter();
}
}
Output :
13
Q 6. Create a class named 'Member' having the following members:
Data members : Name, Age, Phone number, Address, Salary. It also has a
method named 'printSalary' which prints the salary of the members. Two
classes 'Employee' and 'Manager' inherits the 'Member' class. The
'Employee' and 'Manager' classes have data members 'specialization' and
'department' respectively. Now, assign name, age, phone number, address
and salary to an employee and a manager by making an object of both of
these classes and print the same.
Source Code :
class Member {
private String name;
private int age;
private String phoneNumber;
private String address;
private double salary;
public Member(String name, int age, String phoneNumber, String address, double salary)
{
this.name = name;
this.age = age;
this.phoneNumber = phoneNumber;
this.address = address;
this.salary = salary;
}
public void printmember() {
System.out.println(" Name:" + name);
System.out.println("Age:" +age );
System.out.println(" Number:" + phoneNumber);
System.out.println(" Address:" +address );
System.out.println("Salary:" + salary);
14
}
}
}
}
15
super.printmember();
System.out.println("department:" + department);
}
}
16
Q 7. Design a java program using interface, which performs simple
arithmetic such as addition, subtraction, multiplication and division.
Source Code :
interface Calculation
{
public void add();
public void sub();
public void multi();
public void div();
}
public class Arithmetic implements Calculation
{
int a,b;
public Arithmetic(int a, int b)
{
this.a=a;
this.b=b;
}
public void add()
{
System.out.println("Addition of "+a+" and "+b+" is: "+(a+b));
}
public void sub()
{
System.out.println("Subtraction of "+a+" and "+b+" is: "+(a-b));
}
public void multi()
{
17
System.out.println("Multiplication of "+a+" and "+b+" is: "+(a*b));
}
public void div()
{
System.out.println("Division of "+a+" and "+b+" is: "+(a/b));
}
public static void main(String[] args)
{
Arithmetic obj=new Arithmetic(8,10);
obj.add();
obj.sub();
obj.multi();
obj.div();
}
}
Output :
18
Q 8. Write a program having interface A containing two methods meth1()
and meth2(). interface B extends A which contain method meth3() and create
one class which implements B. call all the method and print the output.
Source Code :
interface A
{
void meth1();
void meth2();
}
interface B extends A {
void meth3();
}
class MyClass implements B {
public void meth1() {
System.out.println("Method 1");
}
public void meth2()
{
System.out.println ("Method 2");
}
public void meth3()
{
System.out.println ("Method 3");
}
}
class interface12
{
public static void main(String args[])
{
19
MyClass ob = new MyClass();
ob.meth1();
ob.meth2();
ob.meth3();
System.out.println("Name: Richa,Roll no : 23238");
}
}
Output :
20
Q 9. Write a program to implement the polymorphism by creating one
superclass Beaches and three subclasses Mumbai, Kanyakumari, Mangluru.
Subclasses extend the superclass and override its location() and famousfor()
methods. Call the location and famousfor methods of parent class i.e.
beaches.
Source Code :
class Beaches
{
void location()
{
System.out.println("Location of beaches:");
}
void famousfor()
{
System.out.println("Know which beach is famous for:");
}
}
class Mumbai extends Beaches
{
public void location()
{
System.out.println("Juhu beach located in the western suburbs of Mumbai.");
}
public void famousfor()
{
System.out.println("Juhu is famous for soothing views of sunset.");
}
}
class Kanyakumari extends Beaches
21
{
public void location()
{
System.out.println("Sanguthurai beach located in Kanyakumari.");
}
public void famousfor()
{
System.out.println("Sanguthurai is famous for cleanest and calmest.");
}
}
class Mangluru extends Beaches
{
public void location()
{
System.out.println("Panambur beach located in the Mangalore.");
}
public void famousfor()
{
System.out.println("Panambur is famous for seafoods.");
}
}
public class polymorphism
{
public static void main(String args[])
{
System.out.println("Name: Richa Dubey"+" "+"Rollno:23238");
Beaches b;
b=new Mumbai();
b.location();
22
b.famousfor();
b=new Kanyakumari();
b.location();
b.famousfor();
b=new Mangluru();
b.location();
b.famousfor();
}
}
Output :
23
Q 10. Write a program to implement a user defined exception “NotPrime
Exception”. Read number from command line and cjheck whether the
number is prime or not. If it is prime then display message “Number is
prime” and if it is not prime throw the exception “NotPrimeException”.
Source Code :
public class Prime
{
public static void main(String args[])
{
System.out.println("Name:Richa "+" "+"Rollno:23238");
int num=Integer.parseInt(args[0]);
try
{
int count=0;
for(int i=1;i<=num;i++)
{
if(num%i==0)
{
count++;
}
}
if(count==2)
{
System.out.println("Number is Prime.");
}
else
{
throw new Exception("Number is not prime.");
24
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
Output :
25