Professional Documents
Culture Documents
Record Book
Date:
10/04/22 12:05:20
Program
AIM
W1: To write a Java program that prints all real solutions to the quadratic equation ax2 +bx + c = 0. Read
in a, b, c and use the quadratic formula. If the discriminate b2 -4ac is negative, display a message stating
that there are no real solutions.
ALGORITHM
THEORY
equation.
The determinant tells the nature of the roots.
· If determinant is greater than 0, the roots are real
and different.
· If determinant is equal to 0, the roots are real and equal.
· If determinant is less than 0, the
roots are complex and different
cm
ALGORITHM
rit
STEP 1 : START
on
lin
STEP 7 : Print value of root1 and root2 these are Real and different
STEP 11 : Print value of root1 and root2 the roots are real and equal.
STEP 16 : Print value of root2= RealPart – i imaginaryPart the roots are complex and different
STEP 17 : END
Test Cases:
case = 1
input=
output=
No real roots
7@
case= 2
cm
input=
rit
1
on
3
lin
2
e.a
output=
c.i
Root1:-2.0
n
Root2:-1.0
Source Code
import java.util.Scanner;
System.out.println("Enter a");
int a=s.nextInt();
System.out.println("Enter b");
cmrit.blackbucks.me/course/course_download.php?id=131 2/25
4/10/22, 12:05 PM Lab Record
int b=s.nextInt();
System.out.println("Enter c");
int c=s.nextInt();
double f=b*b-4*a*c;
if(f<0)
else{
double r1=(-b-Math.sqrt(f))/(2*a);
double r2=(-b+Math.sqrt(f))/(2*a);
System.out.println("Root1:"+r1);
System.out.println("Root2:"+r2);
TEST CASES
case = 1
input=
1
7@
output=
No real roots
cm
rit
case= 2
input=
on
lin
output=
e.a
Root1:-2.0
Root2:-1.0
c.i
Program
n
AIM
W1: To write a Java program that prompts the user for an integer and then prints out all prime numbers up
to that integer. (use Scanner class to read input)
ALGORITHM
THEORY
A prime number (or a prime) is a natural number greater than 1 that cannot be formed by
multiplying two
smaller natural numbers. A natural number greater than 1 that is not prime is
called a composite number.
A number which is divided only by 1 and itself is known as the prime
number.
ALGORITHM
STEP 1 : START
cmrit.blackbucks.me/course/course_download.php?id=131 3/25
4/10/22, 12:05 PM Lab Record
STEP 4 : Initialize p to 0
STEP 6 : if j divides i
STEP 8 : p =1,break;
Test Cases:
7@
case = 1
input =
cm
10
rit
output =
on
2
lin
3
e.a
5
c.i
7
n
case =2
input =
15
output =
11
cmrit.blackbucks.me/course/course_download.php?id=131 4/25
4/10/22, 12:05 PM Lab Record
13
Source Code
import java.util.Scanner;
System.out.println("Enter n");
int n=s.nextInt();
int count=0;
if(i%j==0){
count=1;
break;
if(count==0)
System.out.println(i);
7@
cm
TEST CASES
rit
case = 1
input =
on
10
output =
lin
e.a
c.i
case =2
input =
15
output =
11
13
Program
AIM
W2: To write a Java program to create a Student class with following fields I. Hall ticket number II.
Student Name III. Department Create ‘n’ number of Student objects where ‘n’ value is passed as input to
constructor.
cmrit.blackbucks.me/course/course_download.php?id=131 5/25
4/10/22, 12:05 PM Lab Record
ALGORITHM
THEORY
TYPES OF CONSTRUCTORS
1. Default Constructors: Default constructor is the constructor which doesn’t take any
argument. It has no
parameters.
ALGORITHM
STEP 1 : START
7@
STEP 4 : Create parameterized constructor which has 3 parameters. Initialize the instant
variables with
parameters variables of the constructor.
rit
STEP 6 : Create ‘n’ number of Student objects where ‘n’ value is passed as input to constructor
lin
STEP 7 : STOP
e.a
c.i
n
Test Cases:
input=
501
aaa
cse
output=
Roll:501
Name:aaa
Branch:cse
cmrit.blackbucks.me/course/course_download.php?id=131 6/25
4/10/22, 12:05 PM Lab Record
Source Code
import java.util.Scanner;
class Week2a {
int roll;
String name;
String branch;
roll=r;
name=n;
branch=b;
void display(){
System.out.println("Roll:"+roll);
System.out.println("Name:"+name);
System.out.println("Branch:"+branch);
System.out.println("EnterRoll number:");
int r=sc.nextInt();
System.out.println("Enter name:");
String m=sc.next();
7@
System.out.println("Enter branch");
String b=sc.next();
cm
ob.display();
rit
on
TEST CASES
lin
case=1
e.a
input=
501
c.i
aaa
cse
output=
Roll:501
Name:aaa
Branch:cse
VIVA VOICE
Program
AIM
W2: To Write a Java program to demonstrate string comparison using == and equals method.
ALGORITHM
cmrit.blackbucks.me/course/course_download.php?id=131 7/25
4/10/22, 12:05 PM Lab Record
THEORY
In general, both equals () and “==” operator in Java are used to compare objects to check
equality but
here are some of the differences between the two:
1. Main difference between. equals () method and == operator is that one is method and other
is
operator.
2. We can use == operators for reference comparison (address comparison) and. equals ()
method for
content comparison. In simple words, == checks if both objects point to the
same memory location
whereas. equals () evaluates to the comparison of values in the
objects.
ALGORITHM
STEP 1 : START
STEP 6 : If Using equals, the result is true because its only comparing the values given in s1
and s2.
Otherwise it returns false
7@
STEP 7 : STOP
cm
rit
on
Test Cases:
lin
case=1
e.a
input=
Java
c.i
Java
n
output=
false
true
case=2
input=
Java
java
cmrit.blackbucks.me/course/course_download.php?id=131 8/25
4/10/22, 12:05 PM Lab Record
output=
false
false
Source Code
import java.util.Scanner;
System.out.println("Enter String1:");
String s1=s.next();
System.out.println("Enter String2:");
String s2=s.next();
System.out.println(s1==s2);
System.out.println(s1.equals(s2));
}
7@
TEST CASES
cm
case=1
input=
rit
Java
Java
on
output=
lin
false
true
e .ac
case=2
input=
.in
Java
java
output=
false
false
Program
AIM
W3: To Write a Java program that checks whether a given string is a palindrome or not. Ex: MADAM is a
palindrome
ALGORITHM
THEORY
cmrit.blackbucks.me/course/course_download.php?id=131 9/25
4/10/22, 12:05 PM Lab Record
ALGORITHM
STEP 1 : START
STEP 2 : Read the given string from console using scanner class and store in the variable str.
STEP 4 : Reverse the given string and store in the variable ‘rev’
STEP 5 : If ( str = rev) then it results as ‘palindrome’, otherwise it results as ‘not palindrome‘
STEP 6 : END
Test Cases:
7@
case=1
cm
input=madam
rit
case=2
input=string
e .ac
Source Code
import java.util.Scanner;
System.out.println("Enter String");
String s1=s.next();
String s2="";
char c[]=s1.toCharArray();
s2+=c[i];
if(s1.equals(s2))
System.out.println(s1+" is a palindrome");
else
cmrit.blackbucks.me/course/course_download.php?id=131 10/25
4/10/22, 12:05 PM Lab Record
TEST CASES
case=1
input=madam
case=2
input=string
Program
AIM
W3: To Write a Java program for sorting list of names. Read input from command line.
ALGORITHM
7@
THEORY
A sorting algorithm is an algorithm that puts elements of a list in a certain order. The most-used
orders are numerical order and lexicographical order.
cm
ALGORITHM
STEP 1 : START
rit
STEP 4 : Read the data from user into array A[] until n.
e .ac
STEP 7 : begin
STEP 9 : begin
STEP 11 : begin
STEP 15 : A[ i + 1] = temp ;
cmrit.blackbucks.me/course/course_download.php?id=131 11/25
4/10/22, 12:05 PM Lab Record
Test Cases:
case=1
input=
apple
zebra
hat
snake
7@
output=
apple
rit
hat
on
snake
lin
zebra
e .ac
Source Code
.in
import java.util.Arrays;
import java.util.Scanner;
System.out.println("Enter n:");
int n=s.nextInt();
s1[i]=s.next();
Arrays.sort(s1);
System.out.println(s1[i]);
cmrit.blackbucks.me/course/course_download.php?id=131 12/25
4/10/22, 12:05 PM Lab Record
TEST CASES
case=1
input=
apple
zebra
hat
snake
output=
apple
hat
snake
zebra
VIVA VOICE
7@
Program
cm
AIM
rit
W4: To write a java program to demonstrate static member, static method and static block.
on
ALGORITHM
lin
THEORY
e .ac
Static keyword can be used with class, variable, method and block. Static members can access
without
object.
Static Member: A static variable is common to all the instances (or objects) of the class because
it
.in
is a class level variable. only a single copy of static variable is created and shared among all the
instances
of the class. Memory allocation for such variables only happens once when the class is
loaded in the
memory.
Unlike non-static variables, such variables can be accessed directly in static and non-static
methods.
Static Method: static methods can access class variables(static variables) without using
object(instance) of
the class, however non-static methods and non-static variables can only be accessed using objects. Static
methods can be accessed directly in static and non-static
methods.
Syntax: Static keyword followed by
return type, followed by method name.
STATIC RETURN_TYPE METHOD_NAME();
Static Class
A
class can be made static only if it is a nested class.
Nested static class doesn’t need reference of Outer
class
A static class cannot access non-static members of the Outer class
STATIC BLOCK
Static block
is used for initializing the static variables. This block gets executed when the class is
loaded in the
memory. A class can have multiple Static blocks, which will execute in the same
sequence in which they
have been written into the program 1) Static member
cmrit.blackbucks.me/course/course_download.php?id=131 13/25
4/10/22, 12:05 PM Lab Record
ALGORITHM
STEP 1 : START
STEP 3 : Create a static int variable var1 and string variable var2
STEP 6 : Assign the value to these static valuable and string variable 2 times.
STEP 8 : STOP
case=1
input=
7@
output=
cm
static block
rit
staticmember n:7
e
Static method
.ac
Normal method
.in
Source Code
class Week4a {
static{
System.out.println("static block");
Week4a(){
n++; m++;
void m1(){
System.out.println("Normal method");
System.out.println("Static method");
cmrit.blackbucks.me/course/course_download.php?id=131 14/25
4/10/22, 12:05 PM Lab Record
System.out.println("staticmember n:"+Week4a.n);
Week4a.m2();
ob2.m1();
TEST CASES
case=1
input=
output=
static block
7@
staticmember n:7
Static method
Normal method
cm
Program
rit
AIM
on
W4: To write a java program to demonstrate method overloading and method overriding
lin
e
ALGORITHM
.ac
THEORY
.in
Method Overloading is a feature that allows a class to have more than one method having the
same name,
if their argument lists are different
ALGORITHM
STEP 1 : START
STEP 3 : Define three user defined ‘display ()’ methods with different parameter lists
STEP 6 : Using object obj call the display () with corresponding parameter values
STEP 7 : STOP
cmrit.blackbucks.me/course/course_download.php?id=131 15/25
4/10/22, 12:05 PM Lab Record
case=1
input=
output=
Source Code
7@
class Overriding{
void test(){
System.out.println("Parent method");
cm
rit
on
lin
.ac
.in
void test() {
ob.sum(5, 6);
ob.sum(2,3,4);
ob.sum("Java","Programming");
ob.test();
TEST CASES
case=1
input=
cmrit.blackbucks.me/course/course_download.php?id=131 16/25
4/10/22, 12:05 PM Lab Record
output=
Program
AIM
ALGORITHM
ALGORITHM
STEP 1 : START
STEP 11 : call print() and show() using the object ‘obj’ to print the corresponding Messages.
STEP 12 : STOP
case=1
input=
output=
Interface1 method
cmrit.blackbucks.me/course/course_download.php?id=131 17/25
4/10/22, 12:05 PM Lab Record
Interface2 method
Interface3 method
Source Code
interface Interface1{
void m1();
interface Interface2{
void m2();
interface Interface3{
void m3();
System.out.println("Interface1 method");
System.out.println("Interface2 method");
7@
System.out.println("Interface3 method");
cm
ob.m1();
ob.m2();
rit
ob.m3();
on
lin
e .ac
TEST CASES
.in
case=1
input=
output=
Interface1 method
Interface2 method
Interface3 method
Program
AIM
ALGORITHM
THEORY
cmrit.blackbucks.me/course/course_download.php?id=131 18/25
4/10/22, 12:05 PM Lab Record
A java package is a group of similar types of classes, interfaces and sub- packages. Package in
java can be
categorized in two form, built-in package and user- defined package. There are
many built-in packages
such as java, lang, awt, javax, swing, net, io, util, sql etc.
1) Java package is used to categorize the classes and interfaces so that they can be easily
maintained.
ALGORITHM:
STEP 1 : START
STEP 3 : Write a simple java program to print the message “welcome to package” ,import the
package
‘mypack’ and save the file with name Simple.java
STEP 4 : Set the class path to send the class file of A.java source file into classes folder of c:
drive.
STEP 5 : STOP
7@
To Compile:
E:\SOURCES> JAVAC -D C:\CLASSES SIMPLE.JAVA
OOP Through Java
52
S p e c t r u
m
cm
To Run:
To run this program from e:\source directory, you need to set classpath of the directory where the
class file resides.
rit
on
lin
e .ac
case=1
.in
output=Welcome to package
Source Code
package p1;
System.out.println("Welcome to package");
TEST CASES
cmrit.blackbucks.me/course/course_download.php?id=131 19/25
4/10/22, 12:05 PM Lab Record
case=1
output=Welcome to package
VIVA VOICE
Program
AIM
ALGORITHM
THEORY
We can require that some methods be overridden by sub classes by specifying the abstract type
modifier.
These methods are sometimes referred to as sub classer responsibility as they have no
implementation
specified in the super class. Thus a sub class must override them. Any class that
contains one or more
abstract methods must also be declared abstract. Such types of classes are
known as abstract classes.
Abstract classes can contain both abstract and non-abstract methods.
ALGORITHM
STEP 1 : START
7@
STEP 3 : Declare integer variables for height, width and radius in the abstract class Shape.
rit
STEP 5 : Create sub classes Rectangle, Triangle and Circle that extends Shape.
lin
STEP 7 : Invoke the methods in the main class by the respective objects.
.ac
STEP 8 : END
.in
case=1
output=
Source Code
cmrit.blackbucks.me/course/course_download.php?id=131 20/25
4/10/22, 12:05 PM Lab Record
void printArea() {
void printArea() {
void printArea() {
7@
Shape s;
s=r;
cm
s.printArea();
s=t;
s.printArea();
rit
s=c;
on
s.printArea();
lin
e .ac
TEST CASES
.in
case=1
output=
Program
AIM
W4: To write a java program to demonstrate final data, final method and final classes
ALGORITHM
THEORY
cmrit.blackbucks.me/course/course_download.php?id=131 21/25
4/10/22, 12:05 PM Lab Record
The final keyword in java is used to restrict the user. The java final keyword can be used in many
context.
Final can be:
variable
method
class
The final keyword can be applied with the variables, a final
variable that have no value it is called
blank final variable or uninitialized final variable. It can be
initialized in the constructor only. The
blank final variable can be static also which will be initialized in
the static block only.
1) Java final variable: If we make any variable as final, you cannot change the value
of final
variable (It will be constant).
ALGORITHM
STEP 1 : START
STEP 3 : declare a variable final int speedlimit and initialize with value 90.
STEP 4 : Define a user defined method run() to initialize the variable ‘speedlimit’ with 400
STEP 7 : call the run() using obj to display the value of speed limit
STEP 8 : STOP
7@
cm
case=1
rit
input=
on
lin
output=
e.a
final method
Source Code
n
class FinalTest{
System.out.println("final method");
int a=20;
}*/
cmrit.blackbucks.me/course/course_download.php?id=131 22/25
4/10/22, 12:05 PM Lab Record
ob.m1();
TEST CASES
case=1
input=
output=
final method
Program
AIM
W3: To write a Java program to make frequency count of words in a given text.
ALGORITHM
7@
THEORY
cm
Method substring() returns a new string that is a substring of given string. Java String substring()
method
is used to get the substring of a given string based on the passed indexes. There are two
variants of this
method. Find the frequency count of words in a given text.
The substring method has two forms
rit
substring(int beginIndex)
substring(int beginIndex, int endIndex)
on
ALGORITHM
lin
STEP 1 : START
e.a
STEP 2 : Read the sting from the console and store in the variable str using scanner class
c.i
STEP 3 : Read the substring from the console and store in the variable sub using scanner
class object
n
STEP 5 : Search the given substring ‘sub’ in the given string ‘str’ and compute the count of
substring if it
is found.
STEP 7: END
Test Cases:
case=1
cmrit.blackbucks.me/course/course_download.php?id=131 23/25
4/10/22, 12:05 PM Lab Record
input= Java is a popular programming language and Java is used to develop mobile apps and web apps
and desktop apps and games and much more
output=
much:1
apps:3
mobile:1
popular:1
games:1
language:1
web:1
is:2
and:5
Java:2
7@
develop:1
cm
more:1
a:1
rit
on
to:1
programming:1
lin
desktop:1
e.a
used:1
c.i
n
Source Code
import java.util.Set;
import java.util.Hashtable;
import java.util.Scanner;
System.out.println("Enter n:");
int n=s.nextInt();
s1[i]=s.next();
if(ht.containsKey(s1[i]))
cmrit.blackbucks.me/course/course_download.php?id=131 24/25
4/10/22, 12:05 PM Lab Record
ht.put(s1[i],ht.get(s1[i])+1);
else
ht.put(s1[i], 1);
Set<String> s2=ht.keySet();
for(String key:s2)
System.out.println(key+":"+ht.get(key));
TEST CASES
case=1
input=
10
Java
is
Java
and
no
other
7@
is
good
as
cm
Java
rit
output=
other:1
on
as:1
Java:3
lin
good:1
and:1
e.a
is:2
no:1
c.i
n
cmrit.blackbucks.me/course/course_download.php?id=131 25/25