0% found this document useful (0 votes)
14 views25 pages

Java Assignment Richa

java practical assignments MCA

Uploaded by

Richa
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
14 views25 pages

Java Assignment Richa

java practical assignments MCA

Uploaded by

Richa
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 25

Q 1. Installation of jdk environment & following utilities.

What is javac,
javap and javadoc.

Installation of jdk :

Step 1: Download JDK from the Site

 Go to the Oracle site and open the Java SE download page. Under the latest version

of Java Platform, Standard Edition, click on the JDK download button.

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.

Step 2: Install the JDK exe 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

running this file on Windows, we will need Administrator rights.

1
 To begin the installation, we need to double-click on the downloaded file, and we

will be presented with the below window.

 Click on Next to proceed with the installation, and follow the Installation guide

provided for any queries.

 Click on the Close button once the installation has finished.

2
 To recover some of our system’s disk space, it is good practice to delete the

downloaded exe file once the download has been done.

Step 3: Check the Directory

 JDK gets installed in the C directory of our system by default having the path “C:\

Program Files\Java\jdk-11.0”. If we make any change to this path at all, we need to

make a note of it as it will be required in the upcoming steps.

 This is the directory structure for our example.

Step 4: Update the Environment Variables

 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

variable provides us with the library files location.

 If we do not set the PATH variable, we will specify the full path to the JDK bin

every time we run a program.

For example: C:\> “C:\Program Files\Java\jdk-11.0\bin\javac” TestClass.java

 So to set these variables, first right-click on My PC and select Properties.

 Inside Properties, in the left-side panel, select Advanced System Settings, and here

choose the option Environment Variables.

4
 Click on New, and type PATH in the Variable Name, and enter the path of the bin

of installed JDK in the Variable Value field.

 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.

Step 5: Verify the Java Installation

 Open the command prompt and enter the command “java –version”, and if it runs

successfully, Java has been successfully installed.

 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:

public class HelloWorld {


public static void main(String[] args) {
System.out.println("Hello, World!");
}
}

TO compile the java code, you can use ‘javac’

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

javap (Java Disassembler):


To disassemble the HelloWorld.class file generated in the previous step, you can use
javap as follows:

7
This will disassemble the HelloWorld.class file and display the bytecode instructions,
which might look something like this:
Compiled from "HelloWorld.java"

public class HelloWorld {


public HelloWorld();
public static void main(java.lang.String[]);
}

The -c flag is used to display the bytecode instructions.

javadoc (Java Documentation Generator):


To generate API documentation for a Java class, you need to include properly formatted
comments in your source code. Here's an example of a Java class with such comments:
public class Person {
private String name;
public Person(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
To generate documentation for this class, you can run javadoc as follows:

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;

public Rectangle(double length, double breadth){


this.length=length;
this.breadth=breadth;
}
public void area(){
System.out.println("Area is :"+breadth * length);
}
public void perimeter(){
System.out.println("Perimeter is "+2 * (breadth + length));
}
}
class Square extends Rectangle{
public Square(double side){
super(side, side);
}
}
public class Main1{

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
}
}

class Employee extends Member {


private String specialization;

public Employee(String name, int age, String phoneNumber,


String address, double salary, String specialization) {
super(name, age, phoneNumber, address, salary);
this.specialization = specialization;
}
public void printemp()
{
super.printmember();
System.out.println("specialization:" + specialization);

}
}

class Manager extends Member{


private String department;

public Manager(String name, int age, String phoneNumber,


String address, double salary, String department) {
super(name, age, phoneNumber, address, salary);
this.department = department;
}
public void printmanager()
{

15
super.printmember();
System.out.println("department:" + department);

}
}

public class Main2 {


public static void main(String[] args) {
Employee employee = new Employee("Saha", 20, "996548787", "pune", 20000,
"Management");
Manager manager = new Manager("Vicky", 21, "7785469344", "pune",30000, "IT");
System.out.println("Richa Dubey Roll no: 23238 MCA-1");
employee.printemp();
manager.printmanager();
}
}
Output :

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

You might also like