0% found this document useful (0 votes)
3 views12 pages

Java Programs for Basic Concepts

Uploaded by

harshajoddd
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views12 pages

Java Programs for Basic Concepts

Uploaded by

harshajoddd
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Practical No : 07

Write a java program that prompts the user for an integer and
then print out all the prime numbers up to that Integer:

import [Link];

class PrintPrime {
int n;

void getData() {
Scanner sc = new Scanner([Link]);
[Link]("Enter Values: ");
n = [Link]();
}

void showData() {
[Link]("Prime numbers up to " + n + ":
");
for (int i = 2; i <= n; i++) {
int count = 0;
for (int j = 2; j * j <= i; j++) {
if (i % j == 0) {
count++;
break;
}
}
if (count == 0) {
[Link](i + " ");
}
}
[Link]();
}

public static void main(String[] args) {


PrintPrime call = new PrintPrime();
[Link]();
[Link]();
}
}

Output:
Enter Values:
10
Prime numbers up to 10: 2 3 5 7

Practical No : 08

Write a java program for sorting a given list of names in


ascending order.

import [Link].*;
import [Link].*;
public class SortNames {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter the number of names: ");
int n = [Link]();
[Link]();

String names[] = new String[n];


[Link]("Enter the names:");
for (int i = 0; i < n; i++) {
names[i] = [Link]();
}

for (int i = 0; i < n; i++) {


for (int j = i + 1; j < n; j++) {
if (names[i].compareTo(names[j]) > 0) {
String temp = names[i];
names[i] = names[j];
names[j] = temp;
}
}
}

[Link]("Names in alphabetical
order:");
for (int i = 0; i < n; i++) {
[Link](names[i]);
}
}
}

Output:
Enter the number of names: 3
Enter the names: Ram Shyam Abhi
Names in alphabetical order:
Abhi Ram Shyam
Practical No : 09

Write a java program that checks whether a given string is a


palindrome or not. Ex: MADAM is a palindrome.

import [Link].*;

public class Palindrome {


public static void main(String[] args) {
String str = "MADAM", reverseStr = "";

int strLength = [Link]();


for (int i = (strLength - 1); i >= 0; --i) {
reverseStr = reverseStr + [Link](i);
}
if
([Link]().equals([Link]())) {
[Link](str + " is a Palindrome
String");
} else {
[Link](str + " is not a Palindrome
String");
}
}
}

Output:

MADAM is a Palindrome String

Practical No : 10
Write a java program to multiply two given matrices.

import [Link].*;
import [Link].*;

class MatrixMultiplication {
int row,cols;
int arr1[][];
int arr2[][];
int arr3[][];

void getData(){
Scanner sc = new Scanner([Link]);

[Link]("Enter Size Of Rows: ");


row = [Link]();
[Link]("Enter Size Of Column: ");
cols = [Link]();
arr1 = new int [row][cols];
arr2 = new int [row][cols];
arr3 = new int [row][cols];

[Link]("Enter "+row+" Rows & "+cols+" Column


For First Matrix: ");
for (int i = 0; i < row; i++ ){
for(int j = 0; j < cols; j++){
arr1[i][j] = [Link]();
}
}

[Link]("Enter "+row+" Rows & "+cols+" Column


For Second Matrix: ");
for (int i = 0; i < row; i++ ){
for(int j = 0; j < cols; j++){
arr2[i][j] = [Link]();;
}
}
}

void calculation() {
for (int i = 0; i < row; i++) {
for (int j = 0; j < cols; j++) {
arr3[i][j] = 0;
for (int k = 0; k < cols; k++) {
arr3[i][j] += arr1[i][k] * arr2[k][j];
}
}
}
}

void show(){
for (int i = 0; i < row ; i++ ){
for(int j = 0; j < cols; j++){
[Link](+arr3[i][j]+" ");
}
[Link]();
}
}

public static void main(String[] args){


MatrixMultiplication call = new MatrixMultiplication();
[Link]();
[Link]();
[Link]();
}
}

Output:
Enter Size Of Rows: 2

Enter Size Of Column: 2

Enter 2 Rows & 2 Column For First Matrix:


1234
Enter 2 Rows & 2 Column For Second Matrix:
5678

19 22
43 50
Practical No 11

Write a java program for Method overloading and constructor


overloading.

import [Link].*;

class EmployeeD{
int id;
String name;
float salary;

// Default Constructor
public EmployeeD(){
[Link]("Default Constructor!");
}

// Parameterized Constructor
public EmployeeD(int i, String n, float s){
[Link] = i;
[Link] = n;
[Link] = s;
}

// Method Overloading
public void printDetails() {
[Link]("No details provided");
}
public void printDetails(int id, String name, float
salary) {
[Link]("Employee ID: " + id + ",
Name: " + name + ", Salary: " + salary);
}
}

public class ConstructorOverloading {


public static void main(String[] args){
EmployeeD call1 = new EmployeeD();
EmployeeD call = new EmployeeD(1, "Shyam",
4999);

[Link]();
[Link]([Link], [Link],
[Link]);
}
}

Output:
Default Constructor!
No details provided
Employee ID: 1
Name: Shyam
Salary: 4999.0

Practical No : 12

Write a java Program to represent Abstract class with


example:
import [Link].*;
Import [Link].*;

abstract class Shape{


abstract double area();
void displayArea(){
[Link]("Area: "+area());
}
}
class Circle extends Shape{
double radius;
Circle(double radius){
[Link] = radius;
}
double area(){
return [Link] * radius * radius;
}
}

class Rectangle extends Shape{

double length, width;


Rectangle(double length, double width){
[Link] = length;
[Link] = width;
}

double area(){
return length*width;

}
}

public class Abstract{


public static void main(String[] args){
Shape circle = new Circle(5);
Shape rectangle = new Rectangle(4,6);
[Link]();
[Link]();
}
}

Output:

Area: 78.53981633974483
Area: 24.0

Practical No: 13

Write a program to implement multiple inheritance:

import [Link].*;
import [Link].*;

interface Flyable {
void fly();
}

interface Swimmable {
void swim();
}

interface Quackable {
void quack();
}
class Duck implements Flyable, Swimmable, Quackable {
@Override
public void fly() {
[Link]("Duck is flying!");
}

@Override
public void swim() {
[Link]("Duck is swimming!");
}

@Override
public void quack() {
[Link]("Duck is quacking!");
}
}

public class MultipleInheritance {


public static void main(String[] args) {
Duck duck = new Duck();
[Link]();
[Link]();
[Link]();
}
}

Output:

Duck is flying!
Duck is swimming!
Duck is quacking!
Practical No : 14

Write a program to demonstrate method overriding and


super keywords.

import [Link].*;
import [Link].*;

class Animal{
void makeSound(){
[Link]("Animals Makes a Sound!");
}
}

class Dog extends Animal{


@Override

void makeSound(){
[Link]();
[Link]("Dog Barks!");
}
}

public class Overriding {


public static void main(String[] args) {
Animal obj = new Animal();
Dog obj1 = new Dog();
[Link]();
[Link]();
}
}

Output:
Animals Makes a Sound!
Animals Makes a Sound!
Dog Barks!

You might also like