0% found this document useful (0 votes)
4 views

lecture10

The document contains a Java implementation of a simple calculator class with methods for basic arithmetic operations, along with a parameterized test class for testing the calculator using data from a text file. It also includes classes for reading data from both text and Excel files, as well as a JDBC example for connecting to a MySQL database. The overall structure demonstrates the use of object-oriented programming, testing frameworks, and database connectivity in Java.

Uploaded by

gedidaj347
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

lecture10

The document contains a Java implementation of a simple calculator class with methods for basic arithmetic operations, along with a parameterized test class for testing the calculator using data from a text file. It also includes classes for reading data from both text and Excel files, as well as a JDBC example for connecting to a MySQL database. The overall structure demonstrates the use of object-oriented programming, testing frameworks, and database connectivity in Java.

Uploaded by

gedidaj347
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

public class Calculator {

private int input1;


private int input2;

public void setInput(int input1,int input2)


{
this.input1 = input1;
this.input2 = input2;
}

public int getSum()


{
int sum = input1 + input2;
return sum;
}
public int getDiff()
{
int diff = input1 - input2;
return diff;
}
public int getMul()
{
int mul = input1 * input2;
return mul;
}
public int getDiv()
{
int div = input1/input2;
return div;
}
}
@RunWith(Parameterized.class)
public class TestCalculator extends TestCore {

int value1;
int value2;
Calculator c = new Calculator();;

public TestCalculator( int value1, int value2)


{
this.value1 = value1;
//System.out.println(this.value1);
this.value2 = value2;
//System.out.println(this.value2);
}

@Before
public void beforeTest() throws Exception
{
init();

c.setInput(value1, value2);
}
@Test
public void testSum()
{
System.out.println("value1 is -->"+value1+"value2 is -->" + value2);
c.getSum();
}

@Parameters
public static Collection<Object[]> getData() throws Exception
{
//read data from text file

Object[][] data = TextFile.getData();

return Arrays.asList(data);
}
TextFile
public class TextFile {

public static Object[][] getData() throws IOException


{
//connecting stream
File f = new File("d:\\TestData.txt");
FileReader fr = new FileReader(f);
BufferedReader reader = new BufferedReader(fr);

//storing data into arrayList


String line = null;
int rowCount =0;
int columnCount =0;
ArrayList<Integer> myList = new ArrayList<Integer>();
while((line=reader.readLine())!=null)
{
if(line.charAt(0)=='#');
else{
String[] x = line.split("\t");
columnCount=x.length;
for(int i=0;i<x.length;i++)
{
myList.add(Integer.parseInt(x[i]));
}
rowCount++;
}
}

//storing data into Object array


Object[][] array = new Object[rowCount][columnCount];

int var = 0;
for(int i=0;i<array.length;i++)
{
for(int j=0;j<array[i].length;j++)
{
array[i][j] = myList.get(var);
var++;
}
}
reader.close();
return array;
Excel file
public class ExcelFile {

public static Object[][]getData() throws InvalidFormatException, IOException


{
File f = new File("d:\\TestData.xlsx");
FileInputStream fi = new FileInputStream(f);
Workbook workbook = WorkbookFactory.create(fi);
Sheet sheet0 = workbook.getSheetAt(0);
int totalRows = sheet0.getLastRowNum();
}
}
JDBC
public class Copied{
public static void main(String[] args) {
System.out.println("MySQL Connect Example.");
Connection conn = null;
String url = "jdbc:mysql://localhost:3306/";
String dbName = "myDatabase";
String driver = "com.mysql.jdbc.Driver";
String userName = "root";
String password = "";
try {
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url+dbName,userName,password);
System.out.println("Connected to the database");
conn.close();
System.out.println("Disconnected from database");
} catch (Exception e) {
e.printStackTrace();
}
}
}

You might also like