Write a java program to create an abstract class named Shape that contains an empty method named numberOfSides( ). Provide three classes named Rectangle, Triangle and Hexagon such that each one of the classes extends the class Shape. Each one of the classes contains only the method numberOfSides( ) that shows the number of sides in the given geometrical structures. (Exercise to understand polymorphism). //Hexagon.java public class Hexagon extends Shape { public void numberOfSides() { System.out.println( "Hexagon : Number of sides = 6" ); } } //Ploymorphism.java public class PolymorphismDemo { public static void main(String[] args) { Shape[] s = new Shape[ 3 ]; s[ 0 ] = new Rectangle(); s[ 1 ] = new Triangle(); s[ 2 ] = new Hexagon(); for ( int i= 0 ;i<s.length;i++) s[i].numberOfSides(); } } //Rectangle.java public class Rectangle extends Shape { public...
Posts
Showing posts from February, 2023
- Get link
- X
- Other Apps
Question Write a Java program which creates a class named 'Employee' having the following members: Name, Age, Phone number, Address, Salary. It also has a method named 'printSalary( )' which prints the salary of the Employee. Two classes 'Officer' and 'Manager' inherits the 'Employee' class. The 'Officer' and 'Manager' classes have data members 'specialization' and 'department' respectively. Now, assign name, age, phone number, address and salary to an officer and a manager by making an object of both of these classes and print the same. (Exercise to understand inheritance). //Employee.java public class Employee { private String name; private int age; private String phoneNo; private String address; private int salary; public Employee(String name, int age, String phoneNo,String address, int salary) { this .name = name; this .age = age; this .phoneNo ...
Write a Java program that shows the usage of try, catch, throw, throws and finally.
- Get link
- X
- Other Apps
//stack.java class Stack { private int[] a; private int size; private int top; public Stack(int size) { this.size = size; a = new int[size]; top = -1; } public void push(int item) throws StackFull { if(top == size-1) { throw new StackFull("stack is full item cannot be stored"); } { top = top + 1; a[top] = item; } } int pop() throws StackEmpty { if(top == -1) { throw new StackEmpty("stack is empty"); } { int item = a[top]; top = top - 1; return item; } } } //StackDemo.java class StackDemo { public static void main(String[] args) { try { Stack s = new Stack(4); s.push(10); s.push(20); s.push(30); s.push(40); s.push(50); s.pop(); } catch(StackFull e) { System.out.println(e); } catch(StackEmpty e) { System.out.println(e); } } } //StackEmpty.java class StackEmpty extends Exception { public StackEmpty(Stri...
StringTockenization
- Get link
- X
- Other Apps
import java.util.Scanner; public class StringTokenizationDemo { public static void main(String[] args) { Scanner s = new Scanner(System.in); System.out.println( "enter numbers in a single line separated by comma" ); String str = s.nextLine(); String[] token = str.split( "," ); int sum = 0 ; for ( int i= 0 ;i<token.length;i++) { System.out.println(token[i]); sum = sum + Integer.parseInt(token[i]); } System.out.println( "sum = " + sum); } }
MatrixMultiplication
- Get link
- X
- Other Apps
import java.util.Scanner; class MatrixMultiplication { public static void main(String[] args) { Scanner s = new Scanner(System.in); System.out.println( "enter no of rows and columns of the first matrix" ); int rows1 = s.nextInt(); int cols1 = s.nextInt(); System.out.println( "enter no of rows and columns of the second matrix" ); int rows2 = s.nextInt(); int cols2 = s.nextInt(); if (cols1 != rows2) { ...
FrequencyCountDemo
- Get link
- X
- Other Apps
import java.util.Scanner; public class FrequencyCountDemo { public static void main(String[] args) { Scanner s = new Scanner(System.in); System.out.println( "Eneter a string" ); String str = s.nextLine(); System.out.println( "Enter a character" ); char ch = s.next().charAt( 0 ); int count = 0 ; for ( int i= 0 ;i<str.length(); i++) { if (str.charAt(i) == ch) count++; } System.out.println( "frequency = " + count); } }
Paliendrome
- Get link
- X
- Other Apps
import java.util.Scanner; public class PalindromeTest { public static void main(String[] args) { Scanner s = new Scanner(System.in); System.out.println( "Enter a string" ); String str = s.nextLine(); int i = 0 ; int j = str.length() - 1 ; while (i<j) { if (str.charAt(i) != str.charAt(j)) break ; i++; j--; } if (i<j) System.out.println( "not a palindrome" ); else System.out.println( "palindrome" ); } }