[go: up one dir, main page]

0% found this document useful (0 votes)
9 views13 pages

STV Lab Practical

The document outlines a series of programming experiments conducted by a B.Tech-CSE student at Mohanlal Sukhadia University, focusing on Java programming tasks such as calculating the area and perimeter of a circle, reading and matching names, solving quadratic equations, and processing URLs. Each experiment includes a program description, code implementation, and output examples, along with the use of testing tools like JaButi and Jumble for coverage and mutation analysis. The document serves as a comprehensive lab report for the student's software testing and verification course.
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)
9 views13 pages

STV Lab Practical

The document outlines a series of programming experiments conducted by a B.Tech-CSE student at Mohanlal Sukhadia University, focusing on Java programming tasks such as calculating the area and perimeter of a circle, reading and matching names, solving quadratic equations, and processing URLs. Each experiment includes a program description, code implementation, and output examples, along with the use of testing tools like JaButi and Jumble for coverage and mutation analysis. The document serves as a comprehensive lab report for the student's software testing and verification course.
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/ 13

INSTITUTE OF ENGINEERING AND TECHNOLOGY

Mohanlal Sukhadia University, Udaipur


Name- Shivam Chouhan | Class- B.Tech-CSE (VIII Sem) | Subject- STV Lab

INDEX

S.No. Topic Date Grade Signature

1. Write a program that calculates the area and


perimeter of the circle. And find the Coverage
& Test Cases of that program using JaButi
Tool.
2. Write a program which read the first name
and last name from console and matching
with expected result by using JaBuTi.
3. Write a program that takes three double
numbers from the java console representing ,
respectively, the three coefficients a,b, and c
of a quadratic equation.
4. Write a program that reads commercial
website URL from a url from file .you should
expect that the URL starts with www and
ends with .com. retrieve the name of the site
and output it. For instance, if the user inputs
www.yahoo.com, you should output yahoo.
After that find the test cases and coverage
using JaButi.
5. Write a program that reads two words
representing passwords from the java console
and outputs the number of character in the
smaller of the two. For example, if the words
are open and sesame, then the output should
be 4, the length of the shorter word, open.
And test this program using JaButi
6. Calculate the mutation score of programs
given in 1(a) to 1 (f) using jumble Tool

7. Calculate the coverage analysis of programs


given in 1 (a) To 1 (f) using Eclemma Free
open source Tool.
INSTITUTE OF ENGINEERING AND TECHNOLOGY
Mohanlal Sukhadia University, Udaipur
Name- Shivam Chouhan | Class- B.Tech-CSE (VIII Sem) | Subject- STV Lab

Experiment –1
Aim: Write a program that calculates the area and perimeter of the circle. And find the Coverage &
Test Cases of that program using JaButi Tool., ROI and time during the execution of the program.

Introduction:
In this java program, we will read radius of a circle and find their area and perimeter, this
program will be implementing using class and objects. Here value will be reading and printing
through class methods.

In this example we will read radius of a circle and then calculate area, perimeter of a circle.
We will create a class to find the area and perimeter.

In this program we will use Math.PI to use value of PI.

PROGRAM:
/*Java program to create class to calculate area and perimeter of circle.*/
importjava.util.*;

classAreaOfCircle
{
private float radius=0.0f;
private float area=0.0f;
private float perimeter=0.0f;

//function to read radius


public void readRadius()
{
//Scanner class - to read value from keyboard
Scanner sc=new Scanner(System.in);
System.out.print("Enter radius:");
radius=sc.nextFloat(); //to read float value from keyboard
}

//funtction to calculate area


//return value - will return calculated area
public float getArea()

{
area= Math.PI *radius*radius;
return area;
}

//funtction to calculate perimeter


//return value - will return calculated perimeter
public float getPerimeter()
{
perimeter = 2* Math.PI *radius;
INSTITUTE OF ENGINEERING AND TECHNOLOGY
Mohanlal Sukhadia University, Udaipur
Name- Shivam Chouhan | Class- B.Tech-CSE (VIII Sem) | Subject- STV Lab

return perimeter;
}
}
public class circle
{
public static void main(String []s)
{
AreaOfCircle area=new AreaOfCircle();

area.readRadius();
System.out.println("Area of circle:" + area.getArea());
System.out.println("Perimeter of circle:" + area.getPerimeter());
}
}

Code Output:

Compile: javac circle.java


Run: java circle

Output:
Enter radius:15.50
Area of circle:754.385
Perimeter of circle:97.340004
INSTITUTE OF ENGINEERING AND TECHNOLOGY
Mohanlal Sukhadia University, Udaipur
Name- Shivam Chouhan | Class- B.Tech-CSE (VIII Sem) | Subject- STV Lab

Experiment –2
Aim: Write a program which read the first name and last name from console and matching with
expected result by using JaBuTi.

Program:

import java.util.Scanner;
public class FullNameString {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter first name :: ");
String firstName = input.nextLine();
System.out.print("Enter middle name :: ");
String middleName = input.nextLine();
System.out.print("Enter surname :: ");
String lastName = input.nextLine();

// Here StringBuffer is used to combine 3 strings into single


StringBuffer fullName = new StringBuffer();
fullName.append(firstName);
fullName.append(" "); // For space between names
fullName.append(middleName);
fullName.append(" "); // For space between names
fullName.append(lastName);
System.out.println("Hello, " + fullName);
}
}
}

CODE OUTPUT:
C:\>javac FullNameString.java
C:\>java FullNameString
Enter first name :: Rahul
Enter middle name :: S.
Enter surname :: Tamkhane
Hello, Rahul S. Tamkhane
INSTITUTE OF ENGINEERING AND TECHNOLOGY
Mohanlal Sukhadia University, Udaipur
Name- Shivam Chouhan | Class- B.Tech-CSE (VIII Sem) | Subject- STV Lab

Experiment –3
Aim: Write a program that takes three double numbers from the java console representing ,
respectively, the three coefficients a,b, and c of a quadratic equation.

Program:

QuadraticEquationExample1.java

1. import java.util.Scanner;
2. public class QuadraticEquationExample1
3. {
4. public static void main(String[] Strings)
5. {
6. Scanner input = new Scanner(System.in);
7. System.out.print("Enter the value of a: ");
8. double a = input.nextDouble();
9. System.out.print("Enter the value of b: ");
10. double b = input.nextDouble();
11. System.out.print("Enter the value of c: ");
12. double c = input.nextDouble();
13. double d= b * b - 4.0 * a * c;
14. if (d> 0.0)
15. {
16. double r1 = (-b + Math.pow(d, 0.5)) / (2.0 * a);
17. double r2 = (-b - Math.pow(d, 0.5)) / (2.0 * a);
18. System.out.println("The roots are " + r1 + " and " + r2);
19. }
20. else if (d == 0.0)
21. {
22. double r1 = -b / (2.0 * a);
23. System.out.println("The root is " + r1);
24. }
25. else
26. {
27. System.out.println("Roots are not real.");
28. }
29. }
}

Output:
INSTITUTE OF ENGINEERING AND TECHNOLOGY
Mohanlal Sukhadia University, Udaipur
Name- Shivam Chouhan | Class- B.Tech-CSE (VIII Sem) | Subject- STV Lab

Experiment –4
Aim: Write a program that reads commercial website URL from a url from file .you should expect
that the URL starts with www and ends with .com. retrieve the name of the site and output it. For
instance, if the user inputs www.yahoo.com, you should output yahoo. After that find the test cases
and coverage using JaButi.

Program:

// Java program t demonstrate working of URL


importjava.net.MalformedURLException;
import java.net.URL;

public class URLclass1


{
public static void main(String[] args)
throwsMalformedURLException
{

// creates a URL with string representation.


URL url1 =
new URL("https://www.google.co.in/?gfe_rd=cr&ei=ptYq" +
"WK26I4fT8gfth6CACg#q=geeks+for+geeks+java");

// creates a URL with a protocol,hostname,and path


URL url2 = new URL("http", "www.geeksforgeeks.org",
"/jvm-works-jvm-architecture/");

URL url3 = new URL("https://www.google.co.in/search?"+


"q=gnu&rlz=1C1CHZL_enIN71" +
"4IN715&oq=gnu&aqs=chrome..69i57j6" +
"9i60l5.653j0j7&sourceid=chrome&ie=UTF" +
"-8#q=geeks+for+geeks+java");

// print the string representation of the URL.


System.out.println(url1.toString());
System.out.println(url2.toString());
System.out.println();
System.out.println("Different components of the URL3-");

// retrieve the protocol for the URL


System.out.println("Protocol:- " + url3.getProtocol());

// retrieve the hostname of the url


System.out.println("Hostname:- " + url3.getHost());

// retrieve the default port


INSTITUTE OF ENGINEERING AND TECHNOLOGY
Mohanlal Sukhadia University, Udaipur
Name- Shivam Chouhan | Class- B.Tech-CSE (VIII Sem) | Subject- STV Lab

System.out.println("Default port:- " +


url3.getDefaultPort());

// retrieve the query part of URL


System.out.println("Query:- " + url3.getQuery());

// retrieve the path of URL


System.out.println("Path:- " + url3.getPath());

// retrieve the file name


System.out.println("File:- " + url3.getFile());

// retrieve the reference


System.out.println("Reference:- " + url3.getRef());
}
}

OUTPUT:
https://www.google.co.in/?gfe_rd=cr&ei=ptYqWK26I4fT8gfth6CACg#q=geeks+for+geeks+java
https://www.geeksforgeeks.org/jvm-works-jvm-architecture/

Different components of the URL3-


Protocol:- https
Hostname:- www.google.co.in
Default port:- 443
Query:-
q=gnu&rlz=1C1CHZL_enIN714IN715&oq=gnu&aqs=chrome..69i57j69i60l5.653j0j7&sourceid=
chrome&ie=UTF-8
Path:- /search
File:-
/search?q=gnu&rlz=1C1CHZL_enIN714IN715&oq=gnu&aqs=chrome..69i57j69i60l5.653j0j7&s
ourceid=chrome&ie=UTF-8
INSTITUTE OF ENGINEERING AND TECHNOLOGY
Mohanlal Sukhadia University, Udaipur
Name- Shivam Chouhan | Class- B.Tech-CSE (VIII Sem) | Subject- STV Lab

Experiment –5
Aim: Write a program that reads two words representing passwords from the java console and
outputs the number of character in the smaller of the two. For example, if the words are open and
sesame, then the output should be 4, the length of the shorter word, open. And test this program
using JaButi

Program:
import java.util.Scanner;
class Main
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
String strs[] = sc.nextLine().split(" ");
int max_Length = 0;
int indexL = 0;
int max_Frequency = 0;
int indexF = 0;
System.out.println("Input a text in a line:");
for (int i = 0; i < strs.length; i++)
{
if (max_Length < strs[i].length())
{
indexL = i;
max_Length = strs[i].length();
}
int ctr = 0;
for (int j = i; j < strs.length; j++)
{
if (strs[i].equals(strs[j]))
{
ctr++;
}
}
if (max_Frequency < ctr)
{
indexF = i;
max_Frequency = ctr;
}
}
System.out.println("Most frequent text and the word which has the maximum number of
letters:");
System.out.println(strs[indexF] + " " + strs[indexL]);
}
}
OUTPUT:
Thank you for your comment and your participation.
Input a text in a line:
Most frequent text and the word which has the maximum number of letters:
your participation.
INSTITUTE OF ENGINEERING AND TECHNOLOGY
Mohanlal Sukhadia University, Udaipur
Name- Shivam Chouhan | Class- B.Tech-CSE (VIII Sem) | Subject- STV Lab

Experiment –6
Aim: Calculate the mutation score of programs given in 1(a) to 1 (f) using jumble Tool

Program:

int index = 0;
while(true)
{
index++;
if (index == 10)
break;
}
Mutated Code in JAVA
int index = 0;
while (true)
{
index++;
if (index >= 10)
break;
}
Explanation: The mutant code above will pass the Jumble test because change in == to >= does
not affect the output of the code. Execution will stop when index == 10 and since we are increasing
value by 1 and index starts from 0 so the output will remain same.
Example with Jumble
The code written below has been tested with Jumble plugin in Eclipse. The code detects the first
occurrence of a duplicate and returns the value to the calling function. The program is flawed in
many ways which you can try figuring out.
// Java program to illustrate mutation Testing
// The code detects the first occurrence of a
// duplicate and returns the value to the calling
// function
packagetestPackage;

importjava.util.Arrays;
importjava.util.List;
publicclassSampProg
{
protectedintrepeatedNumber(finalList a)
{
intlen = a.size(),i,dup = -1;
int[] arr = newint[len];
for(i=0; i<len; i++)
INSTITUTE OF ENGINEERING AND TECHNOLOGY
Mohanlal Sukhadia University, Udaipur
Name- Shivam Chouhan | Class- B.Tech-CSE (VIII Sem) | Subject- STV Lab

{
arr[i] = a.get(i);
}

Arrays.sort(arr);
try
{
for(i=1; i<len; i++)
{
if(arr[i] == arr[i-1])
{
dup = arr[i];
break;
}
}
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
returndup;
}
}

// Java program to illustrate mutation Testing


// The code detects the first occurrence of a
// duplicate and returns the value to the calling
// function
packagetestPackage;

importjava.util.Arrays;
importjava.util.List;
publicclassSampProg
{
protectedintrepeatedNumber(finalList a)
{
intlen = a.size(),i,dup = -1;
int[] arr = newint[len];
for(i=0; i<len; i++)
{
arr[i] = a.get(i);
}

Arrays.sort(arr);
try
{
for(i=1; i<len; i++)
{
if(arr[i] == arr[i-1])
{
dup = arr[i];
break;
}
}
}
INSTITUTE OF ENGINEERING AND TECHNOLOGY
Mohanlal Sukhadia University, Udaipur
Name- Shivam Chouhan | Class- B.Tech-CSE (VIII Sem) | Subject- STV Lab

catch(Exception e)
{
System.out.println(e.getMessage());
}
returndup;
}
}

After writing the program we create test cases using JUnit in Java. The output obtained on
performing Jumble Analysis is given below:
Mutating testPackage.SampProg
Tests: testPackage.SampProgTest
Mutation points = 11, unit test time limit 2.94s
M FAIL: (testPackage.SampProg.java:8): -1 -> 1
M FAIL: (testPackage.SampProg.java:10): 0 -> 1
.M FAIL: (testPackage.SampProg.java:10): negated conditional
M FAIL: (testPackage.SampProg.java:16): 1 -> 0
M FAIL: (testPackage.SampProg.java:18): 1 -> 0
M FAIL: (testPackage.SampProg.java:18): - -> +
M FAIL: (testPackage.SampProg.java:18): negated conditional
M FAIL: (testPackage.SampProg.java:16): += -> -=
M FAIL: (testPackage.SampProg.java:16): negated conditional
.Jumbling took 7.595s
Score: 18%
INSTITUTE OF ENGINEERING AND TECHNOLOGY
Mohanlal Sukhadia University, Udaipur
Name- Shivam Chouhan | Class- B.Tech-CSE (VIII Sem) | Subject- STV Lab

Experiment –7
Aim: Calculate the coverage analysis of programs given in 1 (a) To 1 (f) using Eclemma Free open
source Tool.

Program:
EclEmma is a free Java code coverage tool for Eclipse, available under the Eclipse Public License. It
brings code coverage analysis directly into the Eclipse workbench:
 Fast develop/test cycle: Launches from within the workbench like JUnit test runs can
directly be analyzed for code coverage.
 Rich coverage analysis: Coverage results are immediately summarized and highlighted in
the Java source code editors.
 Non-invasive: EclEmma does not require modifying your projects or performing any other
setup.
Since version 2.0 EclEmma is based on the JaCoCo code coverage library. The Eclipse integration
has its focus on supporting the individual developer in an highly interactive way. For automated
builds please refer to JaCoCo documentation for integrations with other tools.
Originally EclEmma was inspired by and technically based on the great EMMA library developed by
VladRoubtsov.
The update site for EclEmma is http://update.eclemma.org/. EclEmma is also available via the
Eclipse Marketplace Client, simply search for "EclEmma".
Launching
EclEmma adds a so called launch mode to the Eclipse workbench. It is called Coverage mode and
works exactly like the existing Run and Debug modes. The Coverage launch mode can be activated
from the Run menu or the workbench's toolbar:
EclEmma adds a so called launch mode to the Eclipse workbench. It is called Coverage mode and
Works exactly like the existing Run and Debug modes. The Coverage launch mode can be activated
from the Run menu or the workbench's toolbar:

Simply launch your applications or unit tests in the Coverage mode to collect coverage information.
Currently the following launch types are supported:
 Local Java application
 Eclipse/RCP application
 Equinox OSGi framework
 JUnit test
 TestNG test
 JUnit plug-in test
 JUnit RAP test
 SWTBot test
 Scala application
Analysis
INSTITUTE OF ENGINEERING AND TECHNOLOGY
Mohanlal Sukhadia University, Udaipur
Name- Shivam Chouhan | Class- B.Tech-CSE (VIII Sem) | Subject- STV Lab

On request or after your target application has terminated code coverage information is automatically
available in the
Eclipse workbench:
 Coverage overview: The Coverage view lists coverage summaries for your Java projects,
 allowing
 drill-down to method level.
 Source highlighting: The result of a coverage session is also directly visible in the Java source editors.
 A customizable color code highlights fully, partly and not covered lines.
 This works for your own source code as well as for source attached to instrumented external libraries.
 Additional features support analysis for your test coverage:
 Different counters: Select whether instructions, branches, lines, methods, types
 orcyclomatic complexity should be summarized.
 Multiple coverage sessions: Switching between coverage data from multiple sessions is possible.
 Merge Sessions: If multiple different test runs should be considered for analysis coverage
 sessions can easily be merged.
 Import/Export
 While EclEmma is primarily designed for test runs and analysis within the Eclipse workbench,
it provides some import/export features.
 Execution data import: A wizard allows to import JaCoCo *.exec execution data files
from external launches.
 Coverage report export: Coverage data can be exported in HTML, XML or CSV format or
asJaCoCo execution data files (*.exec).

You might also like