0% found this document useful (0 votes)
46 views7 pages

Programming Lab Experiments

This document contains 8 questions and their solutions in Java programming. Question 1 asks to write a program to input and print an integer. Question 2 asks to multiply two floating point numbers. Question 3 asks to swap two numbers by assigning them to a temporary variable. Question 4 checks if a string is a palindrome. Question 5 finds the largest element in an array. Question 6 prints an object of a class. Question 7 generates a random string of characters. Question 8 demonstrates importing packages to access classes.

Uploaded by

Janhavi Navale
Copyright
© © All Rights Reserved
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
Download as rtf, pdf, or txt
0% found this document useful (0 votes)
46 views7 pages

Programming Lab Experiments

This document contains 8 questions and their solutions in Java programming. Question 1 asks to write a program to input and print an integer. Question 2 asks to multiply two floating point numbers. Question 3 asks to swap two numbers by assigning them to a temporary variable. Question 4 checks if a string is a palindrome. Question 5 finds the largest element in an array. Question 6 prints an object of a class. Question 7 generates a random string of characters. Question 8 demonstrates importing packages to access classes.

Uploaded by

Janhavi Navale
Copyright
© © All Rights Reserved
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
Download as rtf, pdf, or txt
Download as rtf, pdf, or txt
You are on page 1/ 7

Name:Navale Janhavi Maruti

Roll No:CS2236
Subject:Programming Lab
Q1:Write a Java Program to Print an Integer(Entered by the user)

public static void main(String[] args) {


      
      // input from standard input - keyboard
        Scanner reader = new Scanner(System.in);
        System.out.print("Enter a number: ");
        // nextInt() reads the next integer from the keyboard
        int number = reader.nextInt();
        // println() prints the following line to the output screen
        System.out.println("You entered: " + number);
    }
Output:

Q2:Write a Java program to Multiply two Floating Point


Numbers.
public class MultiplyTwoNumbers {

public static void main(String[] args) {

float first = 1.5f;


float second = 2.0f;

float product = first * second;

System.out.println("The product is: " + product);


}
}

Output

The product is: 3.0

Q3:Write a Java program to swap two numbers.

public class SwapNumbers {

public static void main(String[] args) {

float first = 1.20f, second = 2.45f;

System.out.println("--Before swap--");
System.out.println("First number = " + first);
System.out.println("Second number = " + second);

// Value of first is assigned to temporary


float temporary = first;

// Value of second is assigned to first


first = second;

// Value of temporary (which contains the initial value of first) is


assigned to second
second = temporary;

System.out.println("--After swap--");
System.out.println("First number = " + first);
System.out.println("Second number = " + second);
}
}

Output:

--Before swap--
First number = 1.2
Second number = 2.45
--After swap--
First number = 2.45
Second number = 1.2

Q4: Java program to check whether a number is palindrome or


not.

lass Main {
public static void main(String[] args) {

String str = "Radar", reverseStr = "";

int strLength = str.length();

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


reverseStr = reverseStr + str.charAt(i);
}

if (str.toLowerCase().equals(reverseStr.toLowerCase())) {
System.out.println(str + " is a Palindrome String.");
}
else {
System.out.println(str + " is not a Palindrome String.");
}
}
}

Output

Radar is a Palindrome String.

Q5: Java program to find largest element of an array.

public class Largest {

public static void main(String[] args) {


double[] numArray = { 23.4, -34.5, 50.0, 33.5, 55.5, 43.7, 5.7,
-66.5 };
double largest = numArray[0];

for (double num: numArray) {


if(largest < num)
largest = num;
}

System.out.format("Largest element = %.2f", largest);


}
}

Output

Largest element = 55.50

Q6:Java program to print object of a class.

lass Test {

}
class Main {
public static void main(String[] args) {

// create an object of the Test class


Test obj = new Test();

// print the object


System.out.println(obj);
}
}

Output

Test@512ddf17

Q7:Java program to create random strings.

import java.util.Random;

class Main {
public static void main(String[] args) {

// create a string of all characters


String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

// create random string builder


StringBuilder sb = new StringBuilder();

// create an object of Random class


Random random = new Random();

// specify length of random string


int length = 7;

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


// generate random index number
int index = random.nextInt(alphabet.length());

// get character specified by index


// from the string
char randomChar = alphabet.charAt(index);

// append the character to string builder


sb.append(randomChar);
}

String randomString = sb.toString();


System.out.println("Random String is: " + randomString);

}
}

Output

Random String is: IIYOBRK

Q8: Write a Java program to demonstrate packages.


// Java program to demonstrate accessing of members when

// corresponding classes are imported and not imported.

import java.util.Vector;

public class ImportDemo

public ImportDemo()

// java.util.Vector is imported, hence we are


// able to access directly in our code.

Vector newVector = new Vector();

// java.util.ArrayList is not imported, hence

// we were referring to it using the complete

// package.

java.util.ArrayList newList = new java.util.ArrayList();

public static void main(String arg[])

new ImportDemo();

You might also like