exp 3 to 12 (1)
exp 3 to 12 (1)
Experiment 9 :
Aim : Initialize an integer array with ASCII values and print the corresponding character values
in a single row.
Source code:
import java.util.Scanner;
public class Ascii{
public static void main(String[] args){
Scanner s = new Scanner(System.in);
System.out.print("Enter the no. of values: ");
int n = s.nextInt();
int[] asciivalues = new int[n];
for(int i = 0; i < n; i++){
System.out.print("Enter the ascii value " + (i + 1) + " : ");
asciivalues[i] = s.nextInt();
}
for (int i = 0; i < n; i++){
System.out.print((char)asciivalues[i]+ " ");
}
}
}}
Output :
Experiment 10:
Aim : Write a program to reverse the elements of a given 2*2 array. Four integer numbers need
to be passed as Command-Line arguments.
Source code :
public class Reverse2Darray {
public static void main(String[] args){
if(args.length != 4){
System.out.println("Please provide exactly four integer arguments");
return;
}
int[][] array = new int[2][2];
int index = 0;
for(int i = 0; i < 2; i++){
for(int j = 0; j < 2; j++){
array[i][j] = Integer.parseInt(args[index++]);
}
}
System.out.println("The original array:");
printArray(array);
int temp = array[0][0];
array[0][0] = array[1][1];
array[1][1] = temp;
temp = array[0][1];
array[0][1] = array[1][0];
array[1][0] = temp;
System.out.println("The reversed array:");
printArray(array);
}
private static void printArray(int[][] array){
for(int i = 0; i < 2; i++){
for(int j = 0; j < 2; j++){
System.out.print(array[i][j] + " ");
}
System.out.println();
}
}
}
Output :
Experiment 11:
Aim : Using the concept of method overloading, write method for calculating the area of
triangle, circle and rectangle.
Source code :
public class Areacalculator{
public static void main(String[] args){
System.out.println("The Area of circle is :" + Area(2.5));
System.out.println("The Area of rectangle is:" + Area(2.5, 5.0));
System.out.println("The Area of Triangle is:" + Area(4.2, 7.8, true));
}
public static double Area(double radius){
return Math.PI * radius * radius;
}
public static double Area(double base, double height, boolean istriangle){
return 0.5 * base * height;
}
public static double Area(double length, double breadth){
return length * breadth;
}}
Output :
Experiment 12:
Aim : Create a class Box that uses a parameterized constructor to initialize the dimensions of a
box. The dimensions of the Box are width, height, depth. The class should have a method that
can return the volume of the box. Create an object of the Box class and test the functionalities.
Source code:
import java.util.Scanner;
class Box{
private double height, breadth, length;
Box(double length, double breadth, double height){
this.length = length;
this.breadth = breadth;
this.height = height;
}
public double getvolume(){
return length * breadth * height;
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("Enter the length, breadth, and height of the box: ");
double a = sc.nextDouble();
double b = sc.nextDouble();
double c = sc.nextDouble();
Box mybox = new Box(a, b, c);
System.out.println("Volume of the box is: " + mybox.getvolume());
}}
Output:
Viva Question
1."What is method overloading, and how did you use it to calculate the area of different
shapes?"
Answer: Method overloading allows multiple methods with the same name but different
parameters. I used it to calculate areas: Area(double radius) for a circle, Area(double base,
double height) for a rectangle, and Area(double base, double height, boolean isTriangle
Answer: I reversed the 2x2 array by swapping diagonal elements (array[0][0] with array[1][1]
and array[0][1] with array[1][0]).
3."Why are constructors important in OOP, and how did you use one in the Box class?"
Answer: Constructors initialize objects. In the Box class, the constructor takes parameters
(length, breadth, height) to set the box's dimensions.
4. "How does the Scanner class work, and how did you use it to accept user input in the
Box class?"
Answer: The Scanner class reads user input. I used it to get values for the box's dimensions,
which are then passed to the Box constructor to initialize the object.
5."How do you convert ASCII values to characters, and what happens if an invalid ASCII
value is provided?"
Answer: ASCII values are converted to characters using type casting (e.g., (char) 65). If an
invalid value is provided, it may result in an unpredictable or incorrect character based on the
encoding.