Skip to document
This is a Premium Document. Some documents on Studocu are Premium. Upgrade to Premium to unlock it.

Java Programming Exercises With Solutions (PDF)

Materail
Course

Java programming (CSC1016S)

174 Documents
Students shared 174 documents in this course
Academic year: 2023/2024
Uploaded by:
Anonymous Student
This document has been uploaded by a student, just like you, who decided to remain anonymous.
University of Cape Town

Comments

Please sign in or register to post comments.

Preview text

Index

Q .no. Questions Page no. Remark

1 Write a program that implement the concept of Encapsulation. 03-

2

Write a program to demonstrate the concept of function overloading of

Polymorphism.

05-

3 Write a program to demonstrate concept of construction overloading of

Polymorphism. 07-

4 Write a program the use Boolean data type and print the prime number

series up to 50. 09

5

Write a program to print first 10 number of the following Series using

Do-while Loops 0,1,1,2,3,5,8,11.. 10

6 Write a program to check the given number is Armstrong or not 11-

7 Write a program to find the factorial of any given number. 13

8

Write a program to sort the element of One Dimensional Array in

Ascending order 14

9 Write a program for matrix multiplication using input/output Stream. 15-

10 Write a program for matrix addition using input/output Stream. 17-

11 Write a program for matrix transpose using input/output stream class. 19-

12

Write a program to add the element of Vectors as arguments of main

method(Run time ) and rearrange them, and copy it into an array.

21-

13 Write a program to check that the given String is palindrome or not. 23-

14 Write a program to arrange the String in alphabetical order. 25-

15

Write a program for StringBuffer class which perform the all methods

of that class.

27-

16 Write a program to calculate Simple interest using the Wrapper class. 29

17 Write a program to calculate Area of various geometrical figures using

the abstract class. 30-

18

Write a program where Single class implements more than one

interfaces and with help of interface reference variable user call the

methods.

32-

19 WAP that use the multiple catch statements within the try-catch

mechanism. 34

20 WAP where user will create a self- Exception using the “throw”

keyword. 35

21

Write a program for multithread using is Alive(), join() and

synchronized() methods of thread class 36-

22

Write a program to create a package using command and one package

will import another package.

38-

23

Write a program for JDBC to insert the values into the existing table by

using prepared statement.

40-

24 WAP for JDBC to display the records from the existing table. 42-

25 WAP for demonstrate of switch statement ,continue and break. 44-

Q1) Write a program that implement the concept of Encapsulation.

Ans :-

// Java program to demonstrate encapsulation

class Encapsulate {

// private variables declared

// these can only be accessed by public methods of class

private String Name;

private int Roll;

private int Age;

// get methods for age, name and roll

//to access private variables

public int getAge() { return Age; }

public String getName() { return Name; }

public int getRoll() { return Roll; }

// set methods for age, name and roll

//to access private variable Age

public void setAge(int newAge) { Age = newAge; }

public void setName(String newName) { Name = newName; }

public void setRoll(int newRoll) { Roll = newRoll; }

}

public class TestEncapsulation {

public static void main(String[] args)

{ Encapsulate obj = new Encapsulate();

// setting values of the variables

obj("manish");

obj(21);

obj(5527);

// Displaying values of the variables

System.out("Student's name: " +

obj());

System.out("Student's age: " + obj());

System.out("Student's roll: " +

obj());

// Direct access of Roll is not possible

// due to encapsulation

// System.out("Student's roll: " + obj);

}

}

Output :-

Q2) Write a program to demonstrate the concept of function overloading

of Polymorphism.

Ans :-

// Java program to demonstrate concept of function overloading of

Polymorphism

class Adder{

//Method Overloading: changing no. of arguments

static int add(int a,int b){return a+b;}

static int add(int a,int b,int c){return a+b+c;}

//Method Overloading: changing data type of arguments

static double add(double a, double b){return a+b;}

static String add(String a, String b)

{String str = "Four"; return str; }

//Method Overloading: Sequence of data type of arguments

static void disp(String c, int num)

{System.out(c +" "+ num);}

static void disp(int num, String c)

{System.out(num +" "+ c);}

}

class TestOverloading{

public static void main(String[] args){

//Method Overloading: changing no. of arguments

System.out(Adder(11,11));

System.out(Adder(11,11,11));

//Method Overloading: changing data type of arguments

System.out(Adder(12,12));

System.out(Adder("One","Three"));

//Method Overloading: Sequence of data type of arguments

Adder("manish",5527);

Adder(9399,"hello");

}

}

Output:

Output:-

Q4) Write a program the use Boolean data type and print the prime

number series up to 50.

Ans :-

// Java program to print the prime number series up to 50

public class PrimeNumber

{

public static void main(String []args)

{

int num=50,i;

System.out("\n Prime numbers upto 50 :\n");

for(i=2;i<=num;i++)

{

boolean a=true;

for(int j=2;j<=i-1;j++)

{

if(i%j==0)

{ a=false;

break;

}

}

if(a==true)

{ System.out(" "+i);

}

}

System.out();

}

}

Output:-

Q6) Write a program to check the given number is Armstrong or not.

Ans :-

// Java program to find Nth Armstrong Number

import java.util;

public class Armstrong

{

public static void main(String []args)

{ int n,sum=0,count=0;

Scanner input= new Scanner(System);

System.out("\nEnter a number to check Armstrong or

not : ");

int number =input();

int num=number;

// Find total digits in num

while(num!=0)

{ num=num/10;

count++;

}

//Copy the value for number in num

num=number;

// Calculate sum of power of digits

while (num != 0)

{ n=num%10;

sum=sum+(int)Math(n,count);

num=num/10;

}

if (number == sum )

System.out("\n"+number + " is an Armstrong

number ");

else

System.out("\n"+number + " is not an

Armstrong number ");

}

}

Output:-

Q8) Write a program to sort the element of One Dimensional Array in

Ascending order

Ans :-

// Java Program to Sort Array of Integers using Arrays() Method

import java.util;

import java.util;

public class ArraySort{

public static void main(String args[]){

int []arr = new int[7];

Scanner enter = new Scanner(System);

System.out("\nPlease! Enter 7 numbers to perform

sorting:");

for(int i=0; i<arr; i++)

{

arr[i]=enter();

}

// Applying sort() method over to above array

// by passing the array as an argument

Arrays(arr);

System.out("\nSorting in Ascending order :\n");

for(int i=0;i<arr;i++)

{

System.out(" "+arr[i]);

}

System.out( );

}

}

Output:-

Q9) Write a program for matrix multiplication using input/output Stream.

Ans :-

// Java Program for matrix multiplication using input/output Stream

import java.util;

public class MatrixMultiplication{

public static void main(String []args)

{

Scanner input=new Scanner(System);

System.out("Enter number of rows : ");

int r=input();

System.out("Enter number of columns : ");

int c=input();

if(r!=c)

{

System.out("\nSorry! matrix multiplication cannot

be performed..!");

System(0);

}

else

{ int m1[][]=new int[r][c];

int m2[][]=new int[r][c];

int m3[][]=new int[r][c];

int sum;

System.out("Enter the elements of First

matrix row wise: ");

for(int i=0;i<r;i++)

{

for(int j=0;j<c;j++)

{ m1[i][j]=input();

}

}

System.out("Enter the elements of second

matrix row wise: ");

for(int i=0;i<r;i++)

{

for(int j=0;j<c;j++)

{ m2[i][j]=input();

}

}

for(int i=0;i<r;i++)

{

Q10) Write a program for matrix addition using input/output Stream.

Ans :-

// Java Program for matrix addition using input/output Stream

import java.util;

public class MatrixAddition{

public static void main(String []args)

{

Scanner input=new Scanner(System);

System.out("Enter number of rows : ");

int r=input();

System.out("Enter number of columns : ");

int c=input();

int m1[][]=new int[r][c];

int m2[][]=new int[r][c];

int m3[][]=new int[r][c];

System.out("Enter the elements of First matrix

row wise:");

for(int i=0;i<r;i++)

{ for(int j=0;j<c;j++)

{

m1[i][j]=input();

}

}

System.out("Enter the elements of second matrix

row wise:");

for(int i=0;i<r;i++)

{ for(int j=0;j<c;j++)

{

m2[i][j]=input();

}

}

for(int i=0;i<r;i++)

{ for(int j=0;j<c;j++)

{

m3[i][j]=m1[i][j]+m2[i][j];

}

}

System.out("Sum of two matrices : ");

for(int i=0;i<r;i++)

{ for(int j=0;j<c;j++)

{

System.out(" " +m3[i][j]);

}

System.out();

}

input();

}

}

Output: -

Output:-

Q12) Write a program to add the element of Vectors as arguments of main

method(Run time ) and rearrange them, and copy it into an array.

Ans :-

// Java Program to Demonstrate Working of Vector via Creating and

Using It

// Importing required classes

import java.*;

import java.*;

class VectorExample{

public static void main(String[] args)

{ // Size of the Vector

int n = 5;

Scanner input = new Scanner(System);

// Declaring the Vector with initial size n

Vector<Integer> list = new Vector<Integer>(n);

// Appending new elements at the end of the vector

try{ for(int i=0;i<5;i++)

list(Integer(args[i]));

// Printing elements of list

System.out("\n"+list);

// Remove element at index 3

list(3);

//Displaying the vector after deletion

System.out(list);

// iterating over vector elements usign for loop

System.out("Printing the list using list() --

---");

for (int i = 0; i<list(); i++)

// Printing elements one by one

System.out(" "+list(i));

// Creating the array and using toArray()

Object[] arr = list();

System.out("\nPrinting the list using Array() ---

--");

for (int i = 0; i<arr; i++)

System.out(" "+arr[i]);

System.out();

Was this document helpful?
This is a Premium Document. Some documents on Studocu are Premium. Upgrade to Premium to unlock it.

Java Programming Exercises With Solutions (PDF)

Course: Java programming (CSC1016S)

174 Documents
Students shared 174 documents in this course
Was this document helpful?

This is a preview

Do you want full access? Go Premium and unlock all 44 pages
  • Access to all documents

  • Get Unlimited Downloads

  • Improve your grades

Upload

Share your documents to unlock

Already Premium?
https://masterprogramming.in/
1
Index
Q .no. Questions Page no. Remark
1Write a program that implement the concept of Encapsulation. 03-04
2Write a program to demonstrate the concept of function overloading of
Polymorphism. 05-06
3Write a program to demonstrate concept of construction overloading of
Polymorphism. 07-08
4Write a program the use Boolean data type and print the prime number
series up to 50. 09
5Write a program to print first 10 number of the following Series using
Do-while Loops 0,1,1,2,3,5,8,11.. 10
6Write a program to check the given number is Armstrong or not 11-12
7Write a program to find the factorial of any given number. 13
8Write a program to sort the element of One Dimensional Array in
Ascending order 14
9Write a program for matrix multiplication using input/output Stream. 15-16
10 Write a program for matrix addition using input/output Stream. 17-18
11 Write a program for matrix transpose using input/output stream class. 19-20
12 Write a program to add the element of Vectors as arguments of main
method(Run time ) and rearrange them, and copy it into an array. 21-22
13 Write a program to check that the given String is palindrome or not. 23-24
14 Write a program to arrange the String in alphabetical order. 25-26
15 Write a program for StringBuffer class which perform the all methods
of that class. 27-28
16 Write a program to calculate Simple interest using the Wrapper class. 29
17 Write a program to calculate Area of various geometrical figures using
the abstract class. 30-31
18
Write a program where Single class implements more than one
interfaces and with help of interface reference variable user call the
methods.
32-33
19 WAP that use the multiple catch statements within the try-catch
mechanism. 34
20 WAP where user will create a self- Exception using the “throw”
keyword. 35
21 Write a program for multithread using is Alive(), join() and
synchronized() methods of thread class 36-37
22 Write a program to create a package using command and one package
will import another package. 38-39
23 Write a program for JDBC to insert the values into the existing table by
using prepared statement. 40-41
24 WAP for JDBC to display the records from the existing table. 42-43
25 WAP for demonstrate of switch statement ,continue and break. 44-45

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.