Assignment 2
Assignment 2
Section:- CSB-06
Q1)
//Write a java program that reads an integers and then moves every zero to
the right side i.e. towards the end.
import java.util.*;
public class Q1 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int size;
System.out.println("Enter the size of Array:- ");
size = input.nextInt();
int arr[] = new int[size];
System.out.println("Enter the elements of Array:- ");
for(int i = 0;i<size;i++) {
arr[i] = input.nextInt();
}
boolean swap = true;
while(swap){
swap = false;
for(int i = 0;i<size-1;i++) {
if(arr[i] == 0 && arr[i+1] != 0) {
int t = arr[i];
arr[i] = arr[i+1];
arr[i+1] = t;
swap = true;
}
}
}
System.out.println("After Swapping:- ");
System.out.print(Arrays.toString(arr));
}
}
Output:-
Q2)
public class Q2 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int size;
System.out.println("Enter the size of Array:- ");
size = input.nextInt();
int arr[] = new int[size];
System.out.println("Enter the elements of Array:- ");
for(int i = 0;i<size;i++) {
arr[i] = input.nextInt();
}
System.out.println("Enter the kth element:- ");
int key = input.nextInt();
Arrays.sort(arr);
System.out.println(arr[key-1]);
}
}
Output:-
Q3)
import java.util.*;
public class Q3 {
static int reverseDigits(int num)
{
int rev_num = 0;
while (num > 0) {
rev_num = rev_num * 10 + num % 10;
num = num / 10;
}
return rev_num;
}
static int isPalindrome(int n)
{
int rev_n = reverseDigits(n);
if (rev_n == n)
return 1;
else
return 0;
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int num;
System.out.println("Enter the number to check palindrome or not:- ");
num = input.nextInt();
System.out.println(isPalindrome(num) == 1?"Yes, it is a palindrome
":"No, the number is not a palindrome");
}
}
Output:-
Q4)
public class Q4 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int size;
System.out.println("Enter the size of Array:- ");
size = input.nextInt();
int arr[] = new int[size];
System.out.println("Enter the elements of Array:- ");
for(int i = 0;i<size;i++) {
arr[i] = input.nextInt();
}
int result = 0;;
for (int i = 0; i < size-3; i++) {
if(arr[i] == (arr[i+1] + 1) && arr[i+1] == (arr[i+2] + 1)) {
result = 0;
}
}
if(result == 1){
System.out.println("false");
} else {
System.out.println("true");
}
}
}
Output:-
Q5)
//Write a java program to find the number of integers within the range of two
specified numbers x & y and that are divisible by another number, p.
import java.util.Scanner;
public class Q5 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int range1,range2,p;
System.out.println("Enter the Range to check:- ");
range1 = input.nextInt();
range2 = input.nextInt();
System.out.println("Enter divisible number:- ");
p = input.nextInt();
if(range1%p == 0) {
System.out.println("Total number of integers divisible are:- " +
(range2/p - range1/p + 1));
} else {
System.out.println("Total number of integers divisible are:- " +
(range2/p - range1/p));
}
}
}
Output:-
Q6)
//Write a Java program to accept two strings and test if the second string
contains the first one.
import java.util.*;
public class Q6 {
public static boolean is_str_contains(String str1, String str2) {
if (str1 == null || str2 == null) {
throw new IllegalArgumentException("You can't pass null strings as
input.");
}
boolean ans = false;
for (int i = 0; i < str2.length() - 1; i++) {
if (str2.charAt(i) == str1.charAt(0)) {
for (int j = 0; j < str1.length(); j++) {
if ((i + j) < str2.length() && str1.charAt(j) ==
str2.charAt(i + j) && j == str1.length() - 1) {
ans = true;
break;
}
}
}
}
return ans;
}
Output:-
Q7)
//Write a Java program to read two strings from keyboard and compare them
lexicographically.
import java.util.*;
public class Q7 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter First String:- ");
String str1 = input.nextLine();
Output:-
Q8)
import java.util.*;
public class Q7 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter First String:- ");
String str1 = input.nextLine();
Output:-
Q9)
import java.util.*;
public class Q7 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter First String:- ");
String str1 = input.nextLine();
Output:-
Q10)
//Write a java program that read an arbitrary number of even integers that
are in the range 2 to 100 inclusive and counts how many occurrences of each
are entered. Indicate the end of the input by entering -1. After entire input
has been processed, print all of the values that were entered by the user
along with the number of occurrences.
import java.util.*;
import javax.naming.spi.InitialContextFactoryBuilder;
for(int i = 0;i<list.length;i++) {
list[i] = 0;
}
System.out.println(("Enter the list of integers between 2 to 100"));
System.out.println("To stop enter an integer out of the range. i.e.:-
-1");
System.out.println("Enter Integer");
int inclusive = input.nextInt();
while(inclusive >= MININT && inclusive <= MAXINT) {
list[inclusive] = list[inclusive] + 1;
Output:-
Q11)
import java.util.*;
int number = 0;
int size = 5;
int arr[] = new int[size];
System.out.println("Enter 5 elements in the Array:- ");
for(int i = 0;i<size;i++) {
arr[i] = input.nextInt();
}
Output:-
Q12)
/**
* InnerQ12
*/
class NumberOfSixes {
int sixes;
NumberOfSixes() {
sixes = 0;
}
public void setSix() {
sixes++;
}
public int getSix() {
return sixes;
}
}
public class Q12 {
Output:-
Q13)
/* Design and implement a set of classes that define various types of reading
material:
books, novels, magazines, technical journals, textbooks, and so on. Include
data
values that describe various attributes of the material, such as the number
of pages
and the names of the primary characters. Include methods that are named
appropriately for each class and that print an appropriate . Create a main
driver class to instantiate and exercise several of the classes.
*/
import java.util.*;
class Novel
{
int number_Of_Page;//
String names_Of_Primary_Characters;//
Novel()//
{
number_Of_Page = 0;
names_Of_Primary_Characters = "";
}
class Magazines
{
int number_Of_Page;
String names_Of_Primary_Characters;
Magazines()//
{
number_Of_Page = 0;
names_Of_Primary_Characters = "";
}
int getPages()
{
return number_Of_Page;
}
String getCharacters()
{
return names_Of_Primary_Characters;
}
}
class TechnicalJournals
{
int number_Of_Page;
String names_Of_Primary_Characters;
TechnicalJournals()
{
number_Of_Page = 0;
names_Of_Primary_Characters = "";
}
Textbooks()//
{
number_Of_Page = 0;
names_Of_Primary_Characters = "";
}
Output:-
Q14)
import java.util.Scanner;
class SavingsAccount {
SavingsAccount.modifyInterestRate(rate2);
Output:-
Q15)
/* */
class StopWatch {
private long startTime = System.currentTimeMillis();
private long endTime = startTime;
public StopWatch() {
}
UML Diagram