0% found this document useful (0 votes)
30 views20 pages

Java Full Stack Assignment Overview

The document contains Java programming assignments focused on various concepts including language basics, flow control statements, and arrays. It includes multiple code examples demonstrating the implementation of classes for tasks such as calculating sums, checking number properties, and manipulating arrays. The assignments are designed for students to practice and enhance their Java programming skills.

Uploaded by

Yehaa Km
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)
30 views20 pages

Java Full Stack Assignment Overview

The document contains Java programming assignments focused on various concepts including language basics, flow control statements, and arrays. It includes multiple code examples demonstrating the implementation of classes for tasks such as calculating sums, checking number properties, and manipulating arrays. The assignments are designed for students to practice and enhance their Java programming skills.

Uploaded by

Yehaa Km
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

Wipro TalentNext Java Full Stack

Java Fundamentals Hands-on Assignment-1


Date:22-07-2024
BY,Yehaasary KM-IIIrd yr AI&DS
Language Basics:
1) public class Location {
public static void main(String[] args) {

if ([Link] != 2) {
[Link](" pass two arguments ");
return;
}
String company = args[0];

String location = args[1];


[Link](company + " Technologies " + location);
}
}
2) public class Message {
public static void main(String[] args) {
if ([Link] != 1) {
[Link]("pass one argument ");

return;
}
String name = args[0];
[Link]("Welcome " + name);
}

}
3) public class Sum {
public static void main(String[] args) {
if ([Link] != 2) {
[Link](" pass two arguments");
return;
}
int num1 = [Link](args[0]);
int num2 = [Link](args[1]);
int sum = num1 + num2;

[Link]("The sum of " + num1 + " and " + num2 + " is " + sum);
}
}
Flow Control Statements:
1)A)
import [Link];

public class NumberCheck {


public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);

[Link]("Enter an integer: ");


int number = [Link]();

if (number > 0) {
[Link]("The number is positive.");
} else if (number < 0) {
[Link]("The number is negative.");
} else {
[Link]("The number is zero.");
}

[Link]();
}
}

B)
public class LastDigit {
public static boolean lastDigit(int a, int b) {
return a % 10 == b % 10;
}

public static void main(String[] args) {


[Link](lastDigit(27, 57));
[Link](lastDigit(7, 17));
[Link](lastDigit(6, 17));
[Link](lastDigit(3, 113));
[Link](lastDigit(5, 115));

}
}

2) import [Link];

public class OddOrEven {


public static void main(String[] args) {

Scanner scanner = new Scanner([Link]);

[Link]("Enter an integer: ");


int number = [Link]();

if (isEven(number)) {
[Link]("The number is even.");
} else {
[Link]("The number is odd.");
}
[Link]();
}

public static boolean isEven(int number) {


return number % 2 == 0;
}
}

5) import [Link];

public class CharacterType {

public static void main(String[] args) {

Scanner scanner = new Scanner([Link]);

[Link]("Enter a character: ");


char character = [Link]().charAt(0);

if ([Link](character)) {
[Link]("Alphabet");
} else if ([Link](character)) {
[Link]("Digit");
} else {

[Link]("Special Character");
}
}
}

8) import [Link];

public class ColorCode {

public static void main(String[] args) {

Scanner scanner = new Scanner([Link]);


[Link]("Enter a color code: ");
String colorCode = [Link]();

switch (colorCode) {

case "R":
[Link]("Red");
break;
case "B":
[Link]("Blue");
break;

case "G":
[Link]("Green");
break;
case "O":
[Link]("Orange");

break;
case "Y":
[Link]("Yellow");
break;
case "W":

[Link]("White");
break;
default:
[Link]("Invalid Code");
}

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

for (int i = 24; i <= 56; i += 2) {


[Link](i);
}
}
}

12) import [Link];

public class PrimeNumber {

public static void main(String[] args) {

Scanner scanner = new Scanner([Link]);


[Link]("Enter a number: ");
int num = [Link]();

boolean isPrime = true;


if (num <= 1) {
isPrime = false;
} else {

for (int i = 2; i <= [Link](num); i++) {


if (num % i == 0) {
isPrime = false;
break;
}

}
}

14) import [Link];

public class SumOfDigits {

public static void main(String[] args) {

Scanner scanner = new Scanner([Link]);


[Link]("Enter a number: ");

int number = [Link]();

int sum = 0;
while (number > 0) {
sum += number % 10;
number /= 10;

[Link]("Sum of digits: " + sum);


}
}

16) import [Link];

public class ReverseNumber {

public static void main(String[] args) {

Scanner scanner = new Scanner([Link]);


[Link]("Enter a number: ");
int number = [Link]();

int reversedNumber = 0;
while (number != 0) {
int digit = number % 10;
reversedNumber = reversedNumber * 10 + digit;
number /= 10;
}

[Link]("Reversed number: " + reversedNumber);


}
}

Arrays:
2) public class MinMax {
public static void main(String[] args) {
int[] numbers = {23, 45, 12, 78, 34, 89, 5, 56};

int max = findMax(numbers);


int min = findMin(numbers);

[Link]("The maximum value is: " + max);

[Link]("The minimum value is: " + min);


}
public static int findMax(int[] arr) {
int max = arr[0];
for (int i = 1; i < [Link]; i++) {

if (arr[i] > max) {


max = arr[i];
}
}
return max;

public static int findMin(int[] arr) {


int min = arr[0];
for (int i = 1; i < [Link]; i++) {

if (arr[i] < min) {


min = arr[i];
}
}
return min;

}
}
3)
import [Link];

public class ArraySearch {


public static void main(String[] args) {
int[] array = {1, 4, 34, 56, 7};

Scanner scanner = new Scanner([Link]);

[Link]("Enter the number to search: ");


int searchElement = [Link]();

int index = findIndex(array, searchElement);

[Link](index);

[Link]();
}
public static int findIndex(int[] array, int searchElement) {

for (int i = 0; i < [Link]; i++) {


if (array[i] == searchElement) {
return i;
}
}

return -1;
}
}
7) import [Link];

public class Duplicates {


public static void main(String[] args) {

int[] array = {12, 34, 12, 45, 67, 89};


int[] uniqueArray = removeDuplicates(array);

[Link]("Array after removing duplicates: " + [Link](uniqueArray));


}

public static int[] removeDuplicates(int[] array) {


int n = [Link];
[Link](array);
int[] temp = new int[n];

int j = 0;

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


if (array[i] != array[i + 1]) {
temp[j++] = array[i];
}
}
temp[j++] = array[n - 1];

int[] uniqueArray = new int[j];


for (int i = 0; i < j; i++) {
uniqueArray[i] = temp[i];
}

return uniqueArray;
}
}

10) import [Link];

public class Rearrange {


public static void main(String[] args) {
int[] array1 = {1, 0, 1, 0, 0, 1, 1};
int[] array2 = {3, 3, 2};

int[] array3 = {2, 2, 2};

[Link]("Rearranged array1: " + [Link](evenOdd(array1)));


[Link]("Rearranged array2: " + [Link](evenOdd(array2)));
[Link]("Rearranged array3: " + [Link](evenOdd(array3)));
}

public static int[] evenOdd(int[] array) {


int[] rearrangedArray = new int[[Link]];
int index = 0;

for (int num : array) {


if (num % 2 == 0) {
rearrangedArray[index++] = num;
}
}

for (int num : array) {


if (num % 2 != 0) {
rearrangedArray[index++] = num;
}

return rearrangedArray;
}
}
14) public class Array {

public static void main(String[] args) {


if ([Link] != 9) {
[Link]("Please enter 9 integer numbers");
return;
}

int[][] array = new int[3][3];


int index = 0;

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


for (int j = 0; j < 3; j++) {
array[i][j] = [Link](args[index++]);
}
}

[Link]("The given array is:");


for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
[Link](array[i][j] + " ");
}
[Link]();

int max = array[0][0];


for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {

if (array[i][j] > max) {


max = array[i][j];
}
}
}

[Link]("The biggest number in the given array is " + max);


}
}

Common questions

Powered by AI

The `NumberCheck` program outputs 'The number is zero' when the input integer is precisely zero. It uses a conditional statement to evaluate the input: if the number is greater than zero, it outputs 'The number is positive'; if less than zero, 'The number is negative', otherwise it defaults to 'zero' when neither condition is met .

To modify the `Sum` program for input verification, it could include a try-catch block to handle `NumberFormatException`. This block would wrap the parsing of the input strings to integers. If a string cannot be parsed appropriately (i.e., if it's not a valid number), the exception would be caught, and an error message displayed, avoiding program termination on invalid input .

The Java program determines the largest and smallest numbers in an integer array by using two separate methods: `findMax` and `findMin`, which iterate through the array. `findMax` initializes the maximum value with the first element and then iterates through the array, updating the maximum value whenever it finds a larger element. Similarly, `findMin` initializes the minimum value with the first element and updates it whenever it finds a smaller element in the array .

The `Rearrange` program separates even and odd numbers in an array by creating a new array and iterating over the original array twice. In the first pass, it appends all even numbers to the new array. In the second iteration, it appends all odd numbers. This reordering ensures all even numbers are positioned before odd numbers, effectively 'rearranging' the array into segregated groups of even and odd numbers .

The `PrimeNumber` program optimizes checking if a number is prime by iterating only up to the square root of the number rather than to itself. This approach reduces the number of iterations required because factors of a number above its square root would have complementary factors below the square root. Thus, if a divisor is not found by the time it reaches the square root, the number is prime .

The `ColorCode` program is case-sensitive; thus, inputting lowercase color codes would result in the default case executing. Specifically, lowercased inputs would not match any specified case labels (like 'R', 'B', 'G', etc.), leading the program to output 'Invalid Code' since lowercase letters are not among the explicitly checked switches .

The `SumOfDigits` program calculates the sum of digits of a given number by repeatedly extracting the last digit (using modulus 10) and adding it to a running total called `sum`. After adding, it updates the number by removing the last digit (using division by 10). This process continues until the number is reduced to zero, at which point the sum of all digits is printed out .

To enhance the `Message` program for scenarios where no arguments are passed, it can include a more specific message indicating that a user-friendly error occurred. This could involve checking `args.length` and outputting a message like 'Error: Please provide a name as an argument', improving clarity by explicitly stating the requirement for an argument .

The `Duplicates` program removes duplicates from an array using a combination of sorting and temporary storage. It first sorts the array to bring duplicates together, then uses a temporary array to store unique elements, copying only the first occurrence of each element. Finally, it creates a new array containing only these unique values, effectively filtering out duplicates .

The `lastDigit` function determines if two integers have the same last digit by computing the remainder of each integer when divided by 10. It then compares these remainders; if they are equal, it returns `true`, indicating that the last digits are the same. Otherwise, it returns `false` .

You might also like