Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
13 views22 pages

Bcom 4 Sem Java Record

This document is a laboratory manual for a Computer Applications course at SV Arts College, Tirupati, focusing on programming with Java. It includes step-by-step instructions for downloading and installing Java, setting up the path and classpath on Windows, and various Java programming exercises covering topics such as primitive data types, control structures, object-oriented programming, and string manipulation. The manual also provides example code for each exercise to demonstrate the concepts discussed.

Uploaded by

ramuramu090090
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views22 pages

Bcom 4 Sem Java Record

This document is a laboratory manual for a Computer Applications course at SV Arts College, Tirupati, focusing on programming with Java. It includes step-by-step instructions for downloading and installing Java, setting up the path and classpath on Windows, and various Java programming exercises covering topics such as primitive data types, control structures, object-oriented programming, and string manipulation. The manual also provides example code for each exercise to demonstrate the concepts discussed.

Uploaded by

ramuramu090090
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

SV ARTS COLLEGE TIRUPATI

Dept. Of Computer Science

B.A/B.Com Computer Applications

SECOND YEAR SEMESTER IV

COMPUTER APPLICATIONS

LABORATORY MANUAL AND RECORD

PROGRAMMING WITH JAVA

Hall ticket No :

Name of Student :

Cell No :
1. Explain the step wise procedure to download and install the Java on Windows.
I. DOWNLOADING AND INSTALLING JAVA 8

i. Open the browser and type the URL

https://www.oracle.com/in/java/technologies/javase/ javase-jdk8-

downloads.html

ii. First find whether your windows is 32 bit or 64 bit by righ clicking on

This Pc or Computer and click on properties

iii. Scroll down the mouse in the browser to Windows X64 as shown in

below figure. And click on download symbol on the righ side of

Window X64 option.

II. Clikc on check box to accept Oracle terms and BSD license agreement

III. Click on Download button

IV. Oracle asks to sign in to oracle account. Sign in using existing oracle account or

create a new account usig your email id. Once you sign into Oracle account,

down load statrt automatically and the .exe file will be saved in your downloads

folder.

V. Double click on the downloaded file to open the installation wizard.

VI. Click on next to select which features to be installed and directory to install.

Select defulat directory and all the features

VII. Click on next to statrt the installation. Click Finish to complete the installation.

2. Explain how to set path and classpath on Windows platform.


Setting the path

i. Rightclikc on Computer or This PC and click on properties. Go to advanced

system settings

ii. Click on environment variables.

iii. In the user variables, select path variable (if path variable is not there

create new path variable)

iv. Set the path variables as C:\Program Files\Java\jdk1.8.0_251\bin; click ok

two times

v. In the system variables, select path variable (if path variable is not there

create new path variable)

vi. Set the path variables as C:\Program Files\Java\jdk1.8.0_251\bin;

vii. In the system variables, edit classpath variable (if the variable is not there,

create new class path

variable)

vi. Set the class path variables as C:\Program Files\Java\jdk1.8.0_251\lib\* and

click ok 3 times.

3. Write a java program to read your name and date of birth from the keyboard.

import java.util.*;
class Name{

public static void main(){

Scanner s=new Scanner(System.in);

System.out.println("Enter your name :");

String st=s.nextLine();

System.out.println("Enter your age :");

int age=s.nextInt();

System.out.println("Enter your year of birth :");

String yr=s.nextLine();

System.out.println("Name :"+st);

System.out.println("Age :"+age);

System.out.println("Year of birth :"+yr);

4. Program to read and print literals of various primitive data types.

class Primitive{
public static void main(String args[]) {

char a = 'G';

int i = 89;

byte b = 4;

short s = 56;

double d = 4.355453532;

float f = 4.7333434f;

System.out.println("char: " + a);


System.out.println("integer: " + i);
System.out.println("byte: " + b);
System.out.println("short: " + s);
System.out.println("float: " + f);
System.out.println("double: " + d);
}
}

Output:
char: G
integer: 89
byte: 4
short: 56
float: 4.7333436
double: 4.355453532

5. Write a java program to demonstrate final variables and escape sequence characters in java

Demonstrate final variables


class Main {
public static void main(String[] args) {

// create a final variable


final int AGE = 32;

// try to change the final variable


AGE = 45;
System.out.println("Age: " + AGE);
}
}

Escape sequence characters

class escape

public static void main (String args[])

System.out.print(" Open University \r");

System.out.print(" \' Tirupati \' ");

System.out.print(" \" Indai \" \n ");

System. out. print(" Want \t to\t learn \t Java");

6. Write a program to demonstrate type cating in Java

public class TypeCasting {


public static void main(String[] args)

float value = 65.0f ;

byte b = (byte) value ;

short s = (short) value ;

char c = (char) value ;

int i = (int) value ;

System.out.printIn("Examples of Narrowing primitive Type casting...!!");

System.out.printIn("float to short : "+b);

System.out.printIn("float to byte : "+s);

System.out.printIn("float to char : "+c);

System.out.printIn("float to int : "+i);

7. Write a java program to convert numneric values to strings using various

ways.

class NumberstoStringTest

public static void main(String arg[])


{

int i = 32;

String s1 = "" + i; // LINE A

System.out.println("s1 = " + s1);

double d = 876.54;

String s2 = String.valueOf(d); // LINE B

System.out.println("s2 = " + s2);

float f = 156.8f;

String s3 = Float.toString(f); // LINE C

System.out.println("s3 = " + s3);

String s4 = "" + i + d; // LINE D

System.out.println("s4 = " + s4);

int n = s4.indexOf('.');

System.out.println(n + " Digits before decimal point of s4 string.");

System.out.println(s4.length() - n - 1 + " Digits after decimal point of s4 string.");

8. Write a java program that shows how increment and decrement operators will work

public class IncrementDecrement

public static void main(String[] args)


{

int number = 50;

System.out.println("Number is " + number);

number++;

System.out.println("Now, number is " + number);

number--;

System.out.println("Now, number is " + number);

Output:

Number is 50

Now, number is 51

Now, number is 50

9. Write a java program to print the name of the month in a year, when the number of month is

given as input using switch statement and do-while loop. Eg. If input Is 4 it should print April.

import java.util.Scanner;
public class Exercise7 {

public static void main(String[] strings) {


Scanner input = new Scanner(System.in);

int daysmonth= 0;
String Month = "Unknown";

System.out.print("Input a month number: ");


int month = input.nextInt();

System.out.print("Input a year: ");


int year = input.nextInt();

switch (month) {
case 1:
Month = "January";
daysmonth= 31;
break;
case 2:
Month = "February";
if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0))) {
daysmonth= 29;
} else {
daysmonth= 28;
}
break;
case 3:
Month = "March";
daysmonth= 31;
break;
case 4:
Month = "April";
daysmonth= 30;
break;
case 5:
Month = "May";
daysmonth= 31;
break;
case 6:
Month = "June";
daysmonth= 30;
break;
case 7:
Month = "July";
daysmonth= 31;
break;
case 8:
Month = "August";
daysmonth= 31;
break;
case 9:
Month = "September";
daysmonth= 30;
break;
case 10:
Month = "October";
daysmonth= 31;
break;
case 11:
Month = "November";
daysmonth= 30;
break;
case 12:
Month = "December";
daysmonth= 31;
}
System.out.print(Month + " " + year + " has " + daysmonth+ " days\n");
}
}

10. Write a Java Program to find the factorial of given number using for loop.

public class Factorial {

public static void main(String[] args) {

int num = 10;


long factorial = 1;
for(int i = 1; i <= num; ++i)
{
// factorial = factorial * i;
factorial *= i;
}
System.out.printf("Factorial of %d = %d", num, factorial);
}
}

Output

Factorial of 10 = 3628800

11. Write a java program that shows how break and continue work in the for loop and while loop.

Break
class Break1 {
public static void main(String[] args)
{
// Initially loop is set to run from 0-9
for (int i = 0; i < 10; i++) {
// Terminate the loop when i is 5
if (i == 5)
break;
System.out.println("i: " + i);
}
System.out.println("Out of Loop");
}
}

Continue
class continue1 {
public static void main(String args[])
{
for (int i = 0; i < 10; i++) {
// If the number is 2
// skip and continue
if (i == 2)
continue;

System.out.print(i + " ");


}
}
}

12. Write a java program to create a class box with data members width,height, depth and methos
to set and print the state of the objects of box class.

public class Box


{
private double length = 1.0;
private double width = 1.0;
private double height = 1.0;

//constructor
public Box (double l, double w, double h)
{
setLength(l);
setWidth(w);
setHeight(h);
}

//set length method


public void setLength(double l)
{
if(l > 0)
{
length = l;
}
else
{
length = 1.0;
}
}

//set width method


public void setWidth(double w)
{
if(w > 0)
{
width = w;
}
else
{
width = 1.0;
}
}

//set height method


public void setHeight(double h)
{
if(h > 0)
{
height = h;
}
else
{
height = 1.0;
}
}

//calculate area method


public double calculateArea(double length, double width)
{
return (length*width);
}

//calculate volume method


public double calculateVolume(double length, double width, double height)
{
return (length*width*height);
}

//get length method


public String getLength()
{
return String.format("%f", length);
}

//get width method


public String getWidth()
{
return String.format("%f",width);
}

//get height
public String getHeight()
{
return String.format("%f",height);
}

public String toString()


{
return String.format("Length is %s.\nWidth is %s.\nHeight is %s.\n", getLength(), getWidth(),
getHeight());
}
import java.util.Scanner;

public class BoxTest


{
public static void main(String[] args)
{
//Box boxOne, boxTwo, boxThree, boxFour;
double l;
double w;
double h;

Scanner input = new Scanner(System.in);


int[] boxes = new int[4];
System.out.print ("Enter the length of your box:");
l= input.nextDouble();
System.out.print ("Enter the width of your box:");
w= input.nextDouble();
System.out.print ("Enter the height of your box:");
h= input.nextDouble();

Box boxOne = new Box(l, w, h);


System.out.println(boxOne.toString());
System.out.printf("The surface area of the box is %f.\nThe volume of the box is %f.\n",
boxOne.calculateArea(l, w), boxOne.calculateVolume(l, w, h));

}
}
13. Write a java program to box and unbox the primitive data type int.

import java.io.*;

class boxing
{
public static void main (String[] args)
{
// creating an Integer Object
// with value 10.
Integer i = new Integer(10);

// unboxing the Object


int i1 = i;

System.out.println("Value of i: " + i);


System.out.println("Value of i1: " + i1);

//Autoboxing of char
Character gfg = 'a';

// Auto-unboxing of Character
char ch = gfg;
System.out.println("Value of ch: " + ch);
System.out.println("Value of gfg: " + gfg);

}
}

Output:
Value of i: 10
Value of i1: 10
Value of ch: a
Value of gfg: a

14. Wrte a java program that shows how a static variable is shared and updated by
a static and non static method.

class Student{
int rollno;
String name;
static String college = "ttd";
static void change(){
college = "svac";
}

Student(int r, String n){


rollno = r;
name = n;
}

void display(){System.out.println(rollno+" "+name+" "+college);}


}

public class TestStaticMethod


{
public static void main(String args[])
{
Student.change();

Student s1 = new Student(111,"hari");


Student s2 = new Student(222,"latha");
Student s3 = new Student(333,"manu");

s1.display();
s2.display();
s3.display();
}
}

15. Write a program to implement varois methods of String class in Java.

public class StringMethods {

public static void main(String[] args) {

String str = "gopal gas company";


System.out.println(str.length());

String str1 = "Software";


String str2 = "Testing";
System.out.println(str1 + str2);
System.out.println(str1.concat(str2));

String str3 = "java string API";


System.out.println(str3.charAt(0));
System.out.println(str3.charAt(1));
System.out.println(str3.charAt(2));
System.out.println(str3.charAt(3));
System.out.println(str3.charAt(6));

String str11 = "Zeus";


String str12 = "Chinese";
String str13 = "American";
String str14 = "Indian";

System.out.println(str11.compareTo(str12));

System.out.println(str13.compareTo(str14));
/
}
}

16. Program to demonstarate multi-level inheritance.

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()

}
}

Output

Inside display
Inside area
Inside volume
17. Write a program to demonstrate packages in java
/* File name : Animal.java */

package animals;

interface Animal {
public void eat();
public void travel();
}

package animals;
/* File name : MammalInt.java */

public class MammalInt implements Animal {

public void eat() {


System.out.println("Mammal eats");
}

public void travel() {


System.out.println("Mammal travels");
}

public int noOfLegs() {


return 0;
}

public static void main(String args[]) {


MammalInt m = new MammalInt();
m.eat();
m.travel();
}
} Now compile the java files as shown below −
$ javac -d . Animal.java
$ javac -d . MammalInt.java

You might also like