You are on page 1of 7

Java Lab Cycle

1.)
a)In 1980 Charles Babbage requested financial support from the British government to build
his computer. In describing how his computer would tabulate functions ,he gave a explicit
example of the function f(x)=x2+x+41. This is a curious polynomial because it generates only
prime numbers. So now write a java program to print f(x) values from 0 to given number
and check whether it generates all primes.
b)Write a java program to print all the Pythagorean triplets between 1 to 100,use an
efficient algorithm to solve it.
Eg:- {3,4,5},{6,8,10},..........
Hint:- {a,b,c} is Pythagorean triplet if and only if a2+b2=c2
2.)Write a java program to compute the square root of a given number S ,take initial value
i.e., x0=S/2using :-
a )Newton- Raphson method( xnew=xold- ( f(xold)/f1(xold) ) ,where f(x)=x2-S
b)Heron method( xnew=( S/xold)+ xold)
Compare both of them and tell which takes less number of iterations using the above
program.
3.)Perform the given operations on strings.
a)Read a string from user which should contain the First-Name, Middle-Name, Last-Name
and should display in following format.
For eg:-Mohandas Karamchand Gandhi to M.K.Gandhi
b)Write a java program to accept a phone number of 10 digits(In String format) and extract
it into 3 parts as follows:
Area code : first 3 charecters
Exchange code : next 3 charecters
Telephone number : last 4 charecters
4.)Write a java program that creates an Array ,accepts both size and values from user
dynamically and prints it and also perform following operations on it:
a)Reverse the Array and print it.
b)Sort the array using (Insertion Sort) and print it.
5.)Create a class named Point that includes the following:
Point

Data:
x-co-ordinate
y –co-ordinate

Methods: Point(int x,int y)


boolean equals(Point p)
int getX()
int getY()
translate(int x,int y)
6)Imagine that cse students of our KLUNIVERSITY had Laptops of different kind, model and
configuration .write a java program which should gather information about students (id,
details_of_name, his_laptop_model, laptop_operatingsystem) initially and should ask user
to enter one id, validate and verify it ,if perfect print his laptop_ info else print error
message.

7.)About Stack

a)Write a java program to implement a String Stack ADT, name it as MyStack with
following methods

 push(String s)
 MyStack(int size)
 String pop()

b)Rewrite the above program also create another class called EnhancedStack which
inherits above class MyStack and increments its functionality by

 String peep()
 EnhancedStack(int size) //call the super class constructor from this constructor
 void print() // prints the elements in the stack in correct order

8) a)Can u write a java program without using main method? If so, write it .

b)Write an apt java program that demonstrates the use of static block in java

[Hint:-for 8a use Static block concept, If we give a class file to JVM, firstly the control goes to
Static block, then to main program, use /*System.exit(0);*/ to exit from application.]

[Note:- 8b program must be written by taking an example of an application ]

9)Create the classes extenting from NumberSytem hierarchy


(Abstract class)
NumberSystem
Methods:-
NumberSystem(String s)
Decimal Hexa Decimal
Abstract String toDecimal()
Abstract StringtoBinary()
Abstract String toOctal()
Abstract String toHexa()
String toString()
Binary
Consider the Octal

Follow the below points:-


1. For Number_ System class take String object which should contain the number
2. In derived class , if we are overriding the method toXXX() ,where XXX =class_name
then write a message in it that “No need to convert its already existing in same
system”
3. For a given class the respective method must convert to respective system.

also prove the polymorphic behaviour of above problem[i.e ,show that base class reference
can point to derived class object and can call the derived class methods ]

10)

1. Create a package called datastructure which contains a class named MyStack [


implement stack(ADT) in that class].
2. Develop a API document for it , by writing documentation-line comments .Then
using that API document as prototype, try to solve your application.

[cmd:- javadoc filename.java, then open index.html file.]

3. Your main program should import this package & reverse the elements of stack
with using only stack operations(only push(),pop() & use can use more than 1
stack object or references) and print elements of stack.

[Hint:- To reverse a stack use temp stack,]


Interface
11)Create an interface OperatingSystem as follows,

OperatingSystem

Attributes:- String name


String manufactured_By

Methods:-
void boots()
void fileManagement()
void processManagement()
void shutdown()

Windows7 Solaris

WiCorporation)
(Microsoft (Oracle Corporation)

Machintosh Linux

(Apple Corporation) (Open-Source OS)


Follow the points:-

Create an Interface OperatingSystem ,from it implement all different OS classes


Write a java program such that, user should enter the operating system name at
command line
Depending on that it should our program should call boot(),then
processManagement() , fileManagement() and finally shutdown().
Display the OS name in the above methods ,eg:- In boot():- “Solaris is
booting...”same for everything function in every method of each class.

12)Write a java program , which asks user to input an integer for Temperature(i.e, the
current temperature of the city) such that it should print,

1. Too hot if temp greater than 50


2. Hot if temp is between 30 and 50
3. Normal if temp between 20 to 30
4. Cool if temp between 15 to 20
5. Too cool if less than 15

The temperature can have value between -78 to +60 ,if other than these values are
given then your program should throw an exception that , that particular temperature
cannot be on Earth.

13) Rewrite the MyStack class of 7 program so that it should throw an exception ,when
stack is full i.e , ”Stack size exceeded” and if empty it should throw an exception that says
“Stack is empty” .

14)Use the Thread concept to solve the following problem.


write a java program ,that reads a number from command line and creates that no of
threads and in each thread print 1 to 10 numbers in the form
eg:- Thread i : 1
Thread k:5
Thread m:4
...... (so on....) , where (i,k,m) is any number between 1 to user limit.
15.)Write two classes WebBrowser and UserURL,ask user to enter >1 no of site address
and create that particular no of UserURL objects , pass randomly them to WebBrowser
class ,that class should create a separate method for each UserURL object and should say
that particular site is loading for specified amount of time.
eg:- www.kluniversity.in is loading...(Thread i)
www.bing.com is loading...(Thread j)
www.linux.com is loading...(Thread k)
where i, j, k are natural numbers.

Sample Programs:-
1.) Program that accepts String(input) from user(Keyboard)
/** Program that accepts input from user and prints it */
Import java.io.*;
Class User_Input
{
public static void main(String param[]) throws IOException
{
// Create BufferedReader object to accept input from keyboard
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
// ask user to enter data
System.out.println(“Enter your name”);
// accept data from buffer(br-BufferedReader obj)
String name=br.readLine();
/* to accept integer ; int num=Integer.parseInt(br.readLine());
// Integer is one of the Wrapper class , used from type-casting parseInt() convers
//string to that particular Wrapper class type here Integer
// for double; double d=Double.parseDouble(br.readLine()); */
System.out.println(“Yout name is :”+name);
}
}

read() to
Diagram depicting how data is accepted from keyboard readLine(); returnsingle
for String charecter

User types Each


Eg:-a,b,d,* ,3,4 Charecter Buffer:-
Eg:- * Eg:-abcd*

System.in InputStreamReader Buffered Reader

Keyboard
2.)Program demonstrating static block.
/** program that demonstrates the use of static block */
class StaticBlockUsage
{ // static block will always execute first before main method executes
static
{
System.out.println("Hi! Cse-IIIyear");
}
public static void main(String[] param)
{
System.out.println("This is not the 1 statement executed");
}
}
/** In general static block is used to do preliminary operations before main executes */
/** Example:- think of you are writing a java program for some Internet application
then ,as our application demands us to get connected to Internet, so u can place
the code for connecting to Internet in static block and u carry with u r application */
3)Write a program that accepts class name from cmd-line and creates an object of that class
/* D:\java NameToObjectDemo type1 ;execute the program */
class NameToObjectDemo
{
public static void main(String[] param)
{ //forName() method puts the name in Class object
Class c=Class.forName(param[0]);
// Object is super class of every class in java
//c.newInstance() returns Object class object so we
// need to type cast to our required type object
// so we got object and we can access the members of class
(Type1)c.newInstance().hai();
}
}
class Type1
{
public void hai()
{
System.out.println("Type1 class says Hai!");
}
}
Note:- If u had any doubt regarding syntax or signature of anything in java we have 2
popular approaches they are:-
1.) API documentation of entire java library ,go to www.oracle.com and download
2.) Use some IDE(eg:-NetBeans) there u can see syntax and description.
*******

You might also like