APPWEEKK6
APPWEEKK6
APPWEEKK6
1)Write a Java program to create a class called "Person" with a name and age attribute. Create two
instances of the "Person" class, set their attributes using the constructor, and print their name and
age.
PROGRAM:
class Person {
this.name = name;
this.age = age; }
return name; }
return age;
} }
} }
OUTPUT:
21CSC203P ADVANCED PROGRAMMING PRACTICE
2) 2. Write a Java program to create class called "TrafficLight" with attributes for color and duration,
and methods to change the color and check for red or green.
PROGRAM:
class TrafficLight {
color = initialColor;
duration = initialDuration;
color = newColor;
return color;
return duration;
return color.equals("red");
return color.equals("green");
trafficLight.changeColor("green");
OUTPUT:
PROGRAM:
return a + b;
return a + b + c;
}
21CSC203P ADVANCED PROGRAMMING PRACTICE
return a + b;
return a - b;
return a - b;
return a * b; }
return a * b; }
if (b != 0) {
return a / b;
} else {
if (b != 0) {
return a / b;
} else {
21CSC203P ADVANCED PROGRAMMING PRACTICE
OUTPUT:
4. Write a Java program to create a class called Employee with methods called work() and getSalary().
Create a subclass called HRManager that overrides the work() method and adds a new method called
addEmployee().
PROGRAM:
// Employee.java
this.salary = salary;
System.out.println("working as an employee!");
return salary;
// HRManager.java
super(salary);
System.out.println("\nManaging employees");
Copy
21CSC203P ADVANCED PROGRAMMING PRACTICE
// Main class
emp.work();
mgr.work();
mgr.addEmployee();
OUTPUT:
5. Write a Java program to create a class called Shape with methods called getPerimeter()
and getArea(). Create a subclass called Circle that overrides the getPerimeter() and
getArea() methods to calculate the area and perimeter of a circle.
PROGRAM:
class Shape {
return 0.0;
return 0.0;
this.radius = radius;
@Override
@Override
}
21CSC203P ADVANCED PROGRAMMING PRACTICE
OUTPUT:
6. Write a Java program to create an interface Sortable with a method sort() that sorts an
array of integers in ascending order. Create two classes BubbleSort and SelectionSort that
implement the Sortable interface and provide their own implementations of the sort()
method.
PROGRAM:
interface Sortable {
@Override
21CSC203P ADVANCED PROGRAMMING PRACTICE
int n = arr.length;
arr[j + 1] = temp;
@Override
int n = arr.length;
int minIndex = i;
minIndex = j;
}
21CSC203P ADVANCED PROGRAMMING PRACTICE
arr[minIndex] = arr[i];
arr[i] = temp;
bubbleSort.sort(arr);
printArray(arr);
selectionSort.sort(arr2);
printArray(arr2);
System.out.println();
OUTPUT:
7. Write a Java program to create an interface Resizable with methods resize Width(int
width) and resizeHeight(int height) that allow an object to be resized. Create a class
Rectangle that implements the Resizable interface and implements the resize methods.
PROGRAM:
interface Resizable {
this.width = width;
this.height = height;
return width;
return height;
@Override
this.width = width;
@Override
this.height = height;
rectangle.resizeWidth(15);
rectangle.resizeHeight(7);
OUTPUT:
8. Write a Java program to create an interface Flyable with a method called fly_obj(). Create
three classes Spacecraft, Airplane, and Helicopter that implement the Flyable interface.
Implement the fly_obj() method for each of the three classes. Hint :- fly_obj definition –
prints the particular object is flying.
21CSC203P ADVANCED PROGRAMMING PRACTICE
interface Flyable {
void fly_obj();
@Override
@Override
@Override
}
21CSC203P ADVANCED PROGRAMMING PRACTICE
System.out.println("Flying Objects:");
System.out.println("-----------------");
spacecraft.fly_obj();
airplane.fly_obj();
helicopter.fly_obj();
OUTPUT:
9. Write a Java program to have the arithmetic functions defined in different user-defined
packages and incorporate all the packages and perform the function in a single class.
PROGRAM:
package mathoperations;
21CSC203P ADVANCED PROGRAMMING PRACTICE
return a + b;
package mathoperations;
return a - b;
package mathoperations;
return a * b;
package mathoperations;
if (b != 0) {
21CSC203P ADVANCED PROGRAMMING PRACTICE
} else {
package mathoperations;
int num2 = 5;
}}
21CSC203P ADVANCED PROGRAMMING PRACTICE
10. Create two different packages to compute bubblesort and selection sort. Write a Java
program to implement sorting functions in a single class.
PROGRAM:
package bubblesort;
int n = arr.length;
arr[j + 1] = temp;
package selectionsort;
21CSC203P ADVANCED PROGRAMMING PRACTICE
int n = arr.length;
int minIndex = i;
minIndex = j;
arr[minIndex] = arr[i];
arr[i] = temp;
package app;
import java.util.Arrays;
import bubblesort.BubbleSort;
import selectionsort.SelectionSort;
System.out.println("Original arrays:");
BubbleSort.sort(arr1);
SelectionSort.sort(arr2);
OUTPUT:
21CSC203P ADVANCED PROGRAMMING PRACTICE
3 Hackerranker questions
1) A string containing only parentheses is balanced if the following is true: 1. if it is an empty
string 2. if A and B are correct, AB is correct, 3. if A is correct, (A) and {A} and [A] are also
correct.Examples of some correctly balanced strings are: "{}()", "[{()}]", "({()})"Examples of some
unbalanced strings are: "{}(", "({)}", "[[", "}{" etc.Given a string, determine if it is balanced or not.
PROGRAM:
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
line = sc.nextLine();
if (isBalanced(line)) System.out.println("true");
else System.out.println("false");
}
}
}
OUTPUT:
2) Static initialization blocks are executed when the class is loaded, and you can initialize static
variables in those blocks . It's time to test your knowledge of Static initialization blocks. You can
read about it here . You are given a class Solution with a main method. Complete the given code
so that it outputs the area of a parallelogram with breadth and height .You should read the
PROGRAM:
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
}//end of main
}//end of class
OUTPUT:
3) Given a string, , matching the regular expression [A-Za-z !,?._'@]+, split the string into tokens.
We define a token to be one or more consecutive English alphabetic letters. Then, print the
Note: You may find the String.split method helpful in completing this challenge.
21CSC203P ADVANCED PROGRAMMING PRACTICE
PROGRAM:
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
System.out.println(numTokens);
OUTPUT:
21CSC203P ADVANCED PROGRAMMING PRACTICE