17CS2014 - Java Programming Lab URK17CS045: Ex. No. 6 Abstract Classes and Objects
17CS2014 - Java Programming Lab URK17CS045: Ex. No. 6 Abstract Classes and Objects
Question-1 (Hackerrank)
Aim
To create a class called MyBook that inherits from an abstract class Book.
Algorithm
Step 1: Start the program.
Step 2: Create an abstract class called Book.
Step 3: Create a class called MyBook that extends Book, in the class MyBook create a function
setTitle() that sets the value passed in the argument to the member value for the class.
Step 4: In the main class, create objects for MyBook.
Step 5: Input the string.
Step 6: Call the setTitle() function from class setTitle() by passing a String value.
Step 7: Stop the program.
Program
import java.util.*;
abstract class Book{
String title;
abstract void setTitle(String s);
String getTitle(){
return title;
}
}
class MyBook extends Book {
@Override
public void setTitle(String title) {
this.title = title;
}
@Override
public String getTitle() {
return title;
}
}
Output
The title is: A tale of two cities
Aim
To create a class called MyCalculator that implements AdvancedArithmetic.
Algorithm
Step 1: Start the program.
Step 2: Create a class called MyCalculator that implements AdvancedArithmetic interface.
Step 3: In the class create a function that returns the sum of divisors of a number by using a loop
that runs from 2 to the number itself.
Step 4: Call the function from main function.
Step 5: Do the calculation and return the result.
Step 6: Print the required output.
Step 7: Stop the program.
Program
import java.util.*;
interface AdvancedArithmetic{
int divisor_sum(int n);
}
class MyCalculator implements AdvancedArithmetic {
public int divisor_sum(int n) {
int z, sum=0;
for(int i=2; i<=n; i++) {
if(n%i==0) {
sum+=i;
}
}
return sum+1;
}
}
class Solution{
public static void main(String []args){
MyCalculator my_calculator = new MyCalculator();
System.out.print("I implemented: ");
ImplementedInterfaceNames(my_calculator);
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
System.out.print(my_calculator.divisor_sum(n) + "\n");
Output
I implemented: AdvancedArithmetic
12
Aim
To create a class that implements the predefined java CharSequence interface found in the
java.lang.package.
Algorithm
Step 1: Start the program.
Step 2: Create a class called Test that implements CharSequence.
Step 3: In the class Test override all the functions that belong to the interface CharSequence. The
functions include charAt where the element at positon arg0 is returned and length that returns the
length of the char array.
Step 4: In the constructor of the class, convert the String value passed into an charArray using
the toCharArray function. Now create a loop that reverses a char array into another array with
one loop variable runs from array size-1 (last element) to the start. Swap the two elements hence
giving the reverse of the String passed.
Step 5: Have a method that returns the reversed String.
Step 6: In the main function create an object for the class Test and pass a String value
Step 7: Call the getReverse() function and print the result.
Step 8: According to each of the functions of CharSequence do the required, and return the
result.
Step 9: Stop the program.
Program
package ex6;
import java.lang.CharSequence;
reverseString = inputString.toCharArray();
cloneString = reverseString.clone();
int j = reverseString.length-1;
cloneString[j] = reverseString[i];
j--;
System.out.print(cloneString[i]);
@Override
return cloneString[arg0];
@Override
@Override
return cloneString.toString().subSequence(arg0,arg1);
package ex6;
import java.util.Scanner;
String s=null;
s=scan.next();
System.out.println("\n"+t.length());
System.out.println(t.charAt(0));
Output
Result
The programs have been verified and executed successfully.
Video Link
https://youtu.be/OfphZxyehHM