You are on page 1of 17

List of Practicals

1. Java Basics
a. Write a Java program that takes a number as input and prints its multiplication
table upto 10.
b. Write a Java program to display the following pattern.
*****
****
***
**
*
c. Write a Java program to print the area and perimeter of a circle.

2. Use of Operators
a. Write a Java program to add two binary numbers.
b. Write a Java program to convert a decimal number to binary number and vice
versa.
c. Write a Java program to reverse a string.

3. Java Data Types


a. Write a Java program to count the letters, spaces, numbers and other characters
of an input string.
b. Implement a Java function that calculates the sum of digits for a given char array
consisting of the digits '0' to '9'. The function should return the digit sum as a
long value.
c. Find the smallest and largest element from the array

4. Methods and Constructors


a. Designed a class SortData that contains the method asec() and desc().
b. Designed a class that demonstrates the use of constructor and destructor.
c. Write a java program to demonstrate the implementation of abstract class.

5. Inheritance
a. Write a java program to implement single level inheritance.
b. Write a java program to implement method overriding
c. Write a java program to implement multiple inheritance.

INDEX

Practical 1
A] Aim: Write a program which takes a number as input from
user and prints its table up to 10
import java.util.Scanner;
public class prac1 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Input a number: ");
int num1 = in.nextInt();
for (int i=0; i< 10; i++)
{
System.out.println(num1 + " x " + (i+1) + " = " + (num1 *
(i+1)));
}
}
}

output:
B]Aim:- Write a Java program to display the following pattern.
*
**
***
****
*****

public class prog121 {


public static void main(String[] args) {
int i, j;
for (i = 1; i <= 6; i++) {
for (j = 1; j < i; j++) {
System.out.print("*");
}
System.out.println();
}
}
}

Output:
C]Aim:write a program to print area and perimeter of the circle.
Program:
import java.util.Scanner;
public class prog3 {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.println("Enter the radius");
double radius=input.nextDouble();
double perimeter=2*Math.PI*radius;
double area=Math.PI*radius*radius;
System.out.println("perimeter is ="+perimeter);
System.out.println("Area is="+area);
}
}

Output:
Practical 2
A]Aim: Write a Java program to addition of the two binary
numbers.
Program:
package com.company;
import java.util.Scanner;
public class prog2a {
public static void main (String[] args){
Scanner sc = new Scanner(System.in);
int[] arr=new int[8];
int i=0,m=0,n=0,sum=0;

System.out.println ("enter 1st binary number");


int n1 = sc.nextInt();

System.out.println ("enter 2nd binary number");


int n2 = sc.nextInt();

for(i=arr.length-1;i>=0;i--){
m=n1%10;
n=n2%10;
n1=n1/10;
n2=n2/10;
sum=m+n+arr[i];
if(sum==1){
arr[i]=sum;
}
else if(sum==2){
arr[i]=0;
arr[i-1]=1;
}
else if(sum==3){
arr[i]=1;
arr[i-1]=1;
}
else{
arr[i]=0;
}
}
System.out.println("Addition of two binary numbers is");
for(i=0;i<arr.length;i++){
if(i==4){
System.out.print(" ");
}
System.out.print(arr[i]);
}
}
}

Output:

B]Aim: Write a program to convert a decimal number to binary


number and binary to decimal.
Program:
import java.util.Scanner;
public class prog2b {
public static void main(String arg[]){
Scanner sc=new Scanner(System.in);
System.out.println("Enter a decimal number");
int n=sc.nextInt();
System.out.print("Binary number is : ");
binary(n);
}
static void binary(int num){
int i = 0;
int bin[]=new int[100];
bin[0]=0;
while(num>0){
bin[i++] = num%2;
num = num/2;
}

for(int j =i-1;j >= 0;j--){


System.out.print(bin[j]);
}
}
}
Output:

C]Aim: Write a program in Java to reverse a string.


Program:
import java.util.Scanner;
public class prog2c {
public static void main(String args[]){
String original, reverse = "";
Scanner in = new Scanner(System.in);

System.out.println("Enter a string to reverse");


original = in.nextLine();

int length = original.length();

for ( int i = length - 1 ; i >= 0 ; i-- ){


reverse = reverse + original.charAt(i);
}

System.out.println("Reverse of entered string is: "


+ reverse);
}

Output:
Practical 3
A]Aim: Write a Java program to count the letters, spaces,
numbers and other characters of an input string.
Program:
public class prog3a {
public static void main(String[] args){
String test = "core java";
count(test);
}

public static void count(String x){


char[] ch = x.toCharArray();
int letter = 0;
int space = 0;
int num = 0;
int other = 0;
for(int i = 0; i < x.length(); i++){
if(Character.isLetter(ch[i])){
letter ++ ;
}
else if(Character.isDigit(ch[i])){
num ++ ;
}
else if(Character.isSpaceChar(ch[i])){
space ++ ;
}
else{
other ++;
}
}

Output:
B]Aim: Implement a Java function that caculates the sum of
digits for a given char array consisting fo the digits ‘0’ to ‘9’. The
function should return the digit sum as a long value.
Program:
public class prog3b {
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Please enter a number to calculate sum of digits");
int number = sc.nextInt();
int sum = 0;
int input = number;
while (input != 0){
int lastdigit = input % 10;
sum += lastdigit;
input /= 10;
}
System.out.printf("Sum of digits of number %d is %d", number, sum);
sc.close();
}
}

Output:
C]Aim: Find the smallest and largest element from the array.
Program:
public class prog3c {
public static void main(String[] args){
int a[] = new int[] { 23, 34, 13, 64, 72, 90, 10,
15, 9, 27 };
int min = a[0]; // assume first elements as smallest number
int max = a[0]; // assume first elements as largest number
for (int i = 1; i < a.length; i++) // iterate forloop from arrays 1st index (second
element)
{
if (a[i] > max)
{
max = a[i];
}
if (a[i] < min)
{
min = a[i];
}
}
System.out.println("Largest Number in a given array is : " + max);
System.out.println("Smallest Number in a given array is : " + min);
}
}
Output:
Practical 4
A]Aim: Design a class Sort Data that contains the method
asec() and desc().
Program:

import java.util.Arrays;
import java.util.Collections;

public class sortData {


void asec(){
// sample int[] array
Integer[] intArray = {1975, 2003, 1979, 1992, 1983
, 1999, 1987};
// before sorting
System.out.println("Integer[] Array - before sorting : ");
for(Integer iValue : intArray){
System.out.println(iValue);
}
// sorting int[] array in ascending order
Arrays.sort(intArray);
// after sorting
System.out.println("\nInteger[] Array - after sorting in ascending order : ");
for(Integer iValue : intArray) {
System.out.println(iValue);
}
}
void desc(){
// sample int[] array
Integer[] intArray = {1975, 2003, 1979, 1992, 1983, 1999, 1987};
// before sorting
System.out.println("Integer[] Array - before sorting : ");
for(Integer iValue : intArray) {
System.out.println(iValue);
}
// sorting int[] array in descending order
Arrays.sort(intArray, Collections.reverseOrder());
// after sorting
System.out.println("\nInteger[] Array - after sorting in descending order : ");
for(Integer iValue : intArray) {
System.out.println(iValue);
}
}
public static void main(String[] args) {
sortData ai=new sortData();
sortData a2=new sortData();
ai.asec();
a2.desc();
}
}

Output:
B]Aim: Derive a class that demonstrate the use fo constructor
and destructor.
Program:
public class program4b {
program4b(){
System.out.println("Hello");
}
public static void main(String args[]){
program4b c1=new program4b();
c1=null;
System.gc();
}

public void finalize(){


System.out.println("Destroyed");
}

Output:
(C)Aim: Write a java program to demonstrate the
implementation of abstract class.
Program:
abstract class calc
{
abstract int sqr(int n1);
abstract int cube(int n1);
void show()
{
System.out.println("Hello");
}
}
public class pract4 extends calc {
int sqr ( int n1)
{
return n1 * n1;
}

int cube ( int n1)


{
return n1 * n1 * n1;
}

public static void main (String args[])


{
pract4 p1 = new pract4();
System.out.println(p1.sqr(333));
System.out.println(p1.cube(444));
p1.show();
}
}

Output:
Practical 5
A)Aim: Write a java program to implement single level
inheritance.
Program: -
public class Animal {
void eat(){
System.out.println("eating...");
}
}

class Dog extends Animal{


void bark(){System.out.println("barking...");}
}

class TestInheritance{
public static void main(String args[]){
Dog d=new Dog();
d.bark();
d.eat();
}
}

Output:
B)Aim: Write a java program to implement method overriding.
Program: -
public class java5b {

//Overridden method
public void eat(){
System.out.println("Human is eating");
}
}
class Boy extends java5b{
//Overriding method
public void eat(){
System.out.println("Boy is eating");
}
public static void main( String args[]){
Boy obj = new Boy();
obj.eat();
}
}

Output:
C]Aim: Write a java program to implement multiple inheritance.
Program:
interface Backend {

// abstract class
public void connectServer();
}

public class Frontend {


public void responsive(String str) {
System.out.println(str + " can also be used as frontend.");
}
public static void main(String[] args) {

// create object of Language class


Language java = new Language();
java.connectServer();
// call the inherited method of Frontend class
java.responsive(java.language);
}
}

// Language extends Frontend class


// Language implements Backend interface
class Language extends Frontend implements Backend {

String language = "Java";

// implement method of interface


public void connectServer() {
System.out.println(language + " can be used as backend language.");
}
}

Output:

You might also like