COMP421star Lab Report 2
COMP421star Lab Report 2
ASSIGNMENT
OUTPUT Q1:
SOLUTION-
I have solve the problem in python code with comments and screenshot for
easy understanding :)
1)
CODE-
//simple method declaration and return
public class Main
{
public static int min (int a, int b)
{
if (a <= b)
return a;
else
return b;
}
public static void main(String[] args) {
System.out.println( Math.min(3,5) );
}
}
OUTPUT-
OUTPUT-
2)
CODE-
import java.util.Scanner;
public class Main {
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
//get 5 number
System.out.print("Input the first number: ");
double x = in.nextDouble();
System.out.print("Input the second number: ");
double y = in.nextDouble();
System.out.print("Input the third number: ");
double z = in.nextDouble();
System.out.print("The average value is " + average(x, y, z)+"\n" );
}
//function with parameterized method
public static double average(double x, double y, double z)
{
return (x + y + z) / 5;
}
}
OUTPUT-
3-
ass Main{
public static void main(String args[]){
//create array of employee object
Employee[] obj = new Employee[2] ;
//create actual employee object
obj[0] = new Employee();
obj[1] = new Employee();
//assign data to employee objects
obj[0].setData(100,"ABC");
obj[1].setData(200,"XYZ");
//display the employee object data
System.out.println("Employee Object 1:");
obj[0].showData();
System.out.println("Employee Object 2:");
obj[1].showData();
}
}
//Employee class with empId and name as attributes
class Employee{
int empId;
String name;
public void setData(intc,String d){
empId=c;
name=d;
}
public void showData(){
System.out.print("EmpId = "+empId + " " + " Employee Name = "+name);
System.out.println();
}
4-
class Demonstration_108 {
public static int j;
public static void main (String args[ ] ) {
for (int i = 0; i < 4; i++ ) {
try {
switch (i) {
case 0 :
int zero = 0;
j = 999/ zero; // Divide by zero
break;
case 1:
int b[ ] = null;
j = b[0] ; // Null pointer error
break;
case 2:
int c[] = new int [2] ;
j = c[10]; // Array index is out-of-bound
Prepared by : Reviewed / Checked by: Verified by: Approved by:
6
break;
case 3:
char ch = "Java".charAt(9) ;// String index is
out-of-bound
break;
}
} catch (Exception e) {
System.out.println("In Test case#"+i+ "\n");
System.out.println (e.getMessage() );
}
}
}
}
OUTPUT:
In Test case#0
/ by zero
In Test case#1
null
In Test case#2
10
In Test case#3
String index out of range: 9
5-
4 All accurate statements with few mistakes in syntax are written in the program with
correct logical sequence.
3 At least few accurate statements are written with clear syntax are written in the
program.
2 Fewer than 2 accurate statements with syntax are written in the program.