Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
4 views

piyush_Java_Lab_3_Assignment[3]

The document contains multiple Java programs demonstrating various functionalities. It includes a program to check for command line arguments, a program to display characters in alphabetical order, a program to identify characters as alphabets, digits, or special characters, and a program to print the name of a month based on a number input. Each program is accompanied by example outputs and error handling for invalid inputs.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

piyush_Java_Lab_3_Assignment[3]

The document contains multiple Java programs demonstrating various functionalities. It includes a program to check for command line arguments, a program to display characters in alphabetical order, a program to identify characters as alphabets, digits, or special characters, and a program to print the name of a month based on a number input. Each program is accompanied by example outputs and error handling for invalid inputs.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

1. WAP to check if a program has received command line arguments or not.

if the program has not received arguments then print "No Values", else print all the values in a single line
separated by , (comma)
Example 1) Java Example
o/p: No values
code;C:\Users\yadav\AppData\Local\Microsoft\Windows\INetCache\IE\M25B5Q8S
public class Example{
public static void main(String[] args){
if (args.length==0){
System.out.println("NO Values");

}
else { System.out.println(String.join(",",args));

}
}

Program
character22. Initialize
variables in three
a program
and display the characters
alphabetical order. in

public class Alpha {


public static void main(String[] args) {
char char1 = 'B';
char char2 = 'A';
if (char1 < char2) {
System.out.println(char1);
System.out.println(char2);
} else {
System.out.println(char2);
System.out.println(char1);
}
}
}

Program
variable 3 3.
in a Initialize and
program a character
print
'Alphabet'
an alphabhet,if the initialized value is
print 'Digit' if the initialized value
is a number,
and print 'special
initialized value ischaracter' if the
Anything else
public class Character {
public static void main(String[] args) {

char ch = 'b';

if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) {
System.out.println("Alphabet");
} else if (ch >= '0' && ch <= '9') {
System.out.println("Digit");
} else {
System.out.println("Special character");
}
}
}

Program 4
4. Write a program to receive a number and print the corresponding month name.
Example 1)
Java Sample 12
o/p: December
Java Sample 1b
o/p: Invalid month
code:
public class MonthName {
public static void main(String[] args) {
if (args.length != 1) {
System.out.println("Invalid input");
return;
}

try {
int monthNumber = Integer.parseInt(args[0]);

switch (monthNumber) {
case 1:
System.out.println("January");
break;
case 2:
System.out.println("February");
break;
case 3:
System.out.println("March");
break;
case 4:
System.out.println("April");
break;
case 5:
System.out.println("May");
break;
case 6:
System.out.println("June");
break;
case 7:
System.out.println("July");
break;
case 8:
System.out.println("August");
break;
case 9:
System.out.println("September");
break;
case 10:
System.out.println("October");
break;
case 11:
System.out.println("November");
break;
case 12:
System.out.println("December");
break;
default:
System.out.println("Invalid month");
}
} catch (NumberFormatException e) {
System.out.println("Invalid month");
}
}
}

You might also like