17CS2014
–Java Programming Lab URK17CS045
Ex. No. 6 Abstract Classes and Objects
Date of Exercise 14 – 2 - 2019 Mark (5)
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;
}
}
Ex No. 6 | Abstract Classes and Objects 1
17CS2014 –Java Programming Lab URK17CS045
public class Main{
public static void main(String []args){
//Book new_novel=new Book(); This line prHMain.java:25: error: Book is abstract; cannot be instantiated
Scanner sc=new Scanner(System.in);
String title=sc.nextLine();
MyBook new_novel=new MyBook();
new_novel.setTitle(title);
System.out.println("The title is: "+new_novel.getTitle());
sc.close();
}
}
Output
The title is: A tale of two cities
Ex No. 6 | Abstract Classes and Objects 2
17CS2014 –Java Programming Lab URK17CS045
Question-2 (Hackerrank)
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");
Ex No. 6 | Abstract Classes and Objects 3
17CS2014 –Java Programming Lab URK17CS045
sc.close();
}
/*
* ImplementedInterfaceNames method takes an object and prints the name of the interfaces it implemented
*/
static void ImplementedInterfaceNames(Object o){
Class[] theInterfaces = o.getClass().getInterfaces();
for (int i = 0; i < theInterfaces.length; i++){
String interfaceName = theInterfaces[i].getName();
System.out.println(interfaceName);
}
}
}
Output
I implemented: AdvancedArithmetic
12
Ex No. 6 | Abstract Classes and Objects 4
17CS2014 –Java Programming Lab URK17CS045
Question-3
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;
public class Test implements CharSequence{
private String inputString;
private char[] reverseString;
//private String resultReverse;
private char[] cloneString;
Ex No. 6 | Abstract Classes and Objects 5
17CS2014 –Java Programming Lab URK17CS045
Test(String inputString) {
reverseString = inputString.toCharArray();
cloneString = reverseString.clone();
int j = reverseString.length-1;
for(int i=0; i<reverseString.length; i++) {
cloneString[j] = reverseString[i];
j--;
for(int i=0; i<reverseString.length; i++) {
System.out.print(cloneString[i]);
@Override
public char charAt(int arg0) {
// TODO Auto-generated method stub
return cloneString[arg0];
@Override
public int length() {
// TODO Auto-generated method stub
Ex No. 6 | Abstract Classes and Objects 6
17CS2014 –Java Programming Lab URK17CS045
return cloneString.length;
@Override
public CharSequence subSequence(int arg0, int arg1) {
// TODO Auto-generated method stub
return cloneString.toString().subSequence(arg0,arg1);
package ex6;
import java.util.Scanner;
public class MainFn {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String s=null;
System.out.println("Enter the string: ");
s=scan.next();
Test t = new Test(s);
System.out.println("\n"+t.length());
System.out.println(t.charAt(0));
Ex No. 6 | Abstract Classes and Objects 7
17CS2014 –Java Programming Lab URK17CS045
Output
Result
The programs have been verified and executed successfully.
Video Link
https://youtu.be/OfphZxyehHM
Ex No. 6 | Abstract Classes and Objects 8