Skip to content

Commit 0a1196f

Browse files
authored
Basic Java Program for Beginners
1 parent 00152f4 commit 0a1196f

11 files changed

+317
-0
lines changed

Bug3524.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import java.util.Scanner;
2+
public class Bug3524 {
3+
public static void main(String[] param) {
4+
System.out.println("2021503524 - Mugundh J B - 09/09/23 - 2.20 pm");
5+
Scanner in = new Scanner(System.in);
6+
System.out.println("Hello. Please type your name:");
7+
String name = in.nextLine();
8+
System.out.println("Hello "+name);
9+
System.out.println ("Have a, nice day!");
10+
}
11+
}

Temp3524.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import java.util.Scanner;
2+
import java.text.DecimalFormat;
3+
public class Temp3524 {
4+
public static void main(String[] args){
5+
Scanner in = new Scanner(System.in);
6+
System.out.println("2021503524 - Mugundh J B - 09/09/23 - 2.40 pm");
7+
System.out.print("Enter temperature in Fahrenheit: ");
8+
float fah = in.nextFloat();
9+
float c = (fah - 32) * 5 / 9;
10+
DecimalFormat df = new DecimalFormat("0.00"); // Used to format the decimal value in form of pattern
11+
System.out.printf("Fahrenheit temperature " + df.format(fah) +
12+
" is the same \nas " + df.format(c) + " degrees Celsius.");
13+
}
14+
}

Verbalize3524.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import java.time.LocalDate;
2+
import java.time.LocalTime;
3+
import java.util.Scanner;
4+
public class Verbalize3524{
5+
// Arrays to store the verbal representations of units and tens
6+
private static final String[] units = {
7+
"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten",
8+
"Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"
9+
};
10+
private static final String[] tens = {
11+
"", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"
12+
};
13+
public static void main(String[] args) {
14+
System.out.println("2021503524 - Mugundh J B");
15+
System.out.println("Date: " + LocalDate.now() + " Time: " + LocalTime.now());
16+
Scanner scanner = new Scanner(System.in);
17+
System.out.print("Enter a number between 1 and 9999: ");
18+
int number = scanner.nextInt();
19+
// Check for invalid input
20+
while (number < 1 || number > 9999) {
21+
System.out.print("Invalid input. Please enter a number between 1 and 9999: ");
22+
number = scanner.nextInt();
23+
}
24+
// Convert the number to its verbal representation
25+
String verbalized = convertToWords(number);
26+
System.out.printf("Verbalized form:\n" + verbalized);
27+
}
28+
// Convert a number to its verbal representation
29+
public static String convertToWords(int number) {
30+
if (number < 20) {
31+
return units[number]; // Directly use the units array
32+
} else if (number < 100) {
33+
return tens[number / 10] + " " + units[number % 10]; // Combine tens and units
34+
} else if (number < 1000) {
35+
return units[number / 100] + " Hundred and " + convertToWords(number % 100); // Handling hundreds by recursion
36+
} else {
37+
return units[number / 1000] + " Thousand and " + convertToWords(number % 1000); // Handling thousands by recursion
38+
}
39+
}
40+
}

dataType3524.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
public class dataType3524 {
2+
// // Integer data types
3+
// byte byteValue = 127; // Correct
4+
// // byte byteValue = 128; // Incorrect: Value exceeds the valid range (-128 to 127)
5+
// long longValue = 9223372036854775807; // Incorrect, require L at the end
6+
// long longValue = 9223372036854775807L; // Correct
7+
// long longValue = 9223372036854775807; // Incorrect, require L at the end
8+
// long longValue = 9223372036854775808L; // Incorrect: Value exceeds the valid range
9+
// // Floating-point data types
10+
// float floatValue = 3.14f; // Correct
11+
// float floatValue = 3.14; // Incorrect: Floating-point literals need 'f' or 'F' suffix
12+
// // Boolean data type
13+
// double doubleValue = 3.14159; // Correct
14+
// // Character data type
15+
// char charValue = 'A'; // Correct
16+
// char charValue = "A"; // Incorrect: Use single quotes for characters
17+
// boolean booleanValue = true; // Correct
18+
// boolean booleanValue = 0; // Incorrect: Use 'true' or 'false' for boolean values
19+
}

dateTime3524.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import java.time.LocalDateTime;
2+
import java.time.format.DateTimeFormatter;
3+
4+
public class dateTime3524 {
5+
public static void main(String[] args) {
6+
System.out.println("2021503524 - Mugundh J B \n");
7+
LocalDateTime current = LocalDateTime.now(); //"LocalDateTime" is the class
8+
//"now()" is a method, represent the current date and time
9+
DateTimeFormatter dateformat = DateTimeFormatter.ofPattern("yyyy-MM-dd");
10+
DateTimeFormatter timeformat = DateTimeFormatter.ofPattern("HH:mm:ss");
11+
// ofPattern() is used to format the date in a particular pattern
12+
String formattedDate = current.format(dateformat);
13+
System.out.print("Date: " + formattedDate);
14+
String formattedTime = current.format(timeformat);
15+
System.out.println(" Time: " + formattedTime);
16+
}
17+
}

digitalWatch3524.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import java.time.LocalDate;
2+
import java.time.LocalTime;
3+
import java.util.Scanner;
4+
public class digitalWatch3524 {
5+
public static void main(String[] args) {
6+
System.out.println("2021503524 - Mugundh J B");
7+
System.out.println("Date: " + LocalDate.now() + " Time: " + LocalTime.now());
8+
Scanner scanner = new Scanner(System.in);
9+
// Prompt the user for input
10+
System.out.print("Input day (Mon, Tue, Wed, Thu, Fri, Sat, Sun): ");
11+
String day = scanner.nextLine();
12+
System.out.print("Input hour (24-hour): ");
13+
int hour = scanner.nextInt();
14+
System.out.print("Input minute: ");
15+
int minute = scanner.nextInt();
16+
System.out.print("Input second: ");
17+
int second = scanner.nextInt();
18+
// Validating day input
19+
if (!isValidDay(day)) {
20+
System.out.println("Invalid day input!");
21+
} else if (hour < 0 || hour > 23 || minute < 0 || minute > 59 || second < 0 || second > 59) {
22+
System.out.println("Invalid time input!");
23+
} else {
24+
// Advancing time by one second
25+
second++;
26+
if (second == 60) {
27+
second = 0;
28+
minute++;
29+
if (minute == 60) {
30+
minute = 0;
31+
hour++;
32+
if (hour == 24) {
33+
hour = 0;
34+
day = getNextDay(day);
35+
}
36+
}
37+
}
38+
// Displaying the new time
39+
System.out.println("New time: " + day + " " + hour + " " + minute + " " + second);
40+
}
41+
}
42+
// Checking if the given day is valid
43+
public static boolean isValidDay(String day) {
44+
return day.equals("Mon") || day.equals("Tue") || day.equals("Wed") ||
45+
day.equals("Thu") || day.equals("Fri") || day.equals("Sat") || day.equals("Sun");
46+
}
47+
// Getting the next day
48+
public static String getNextDay(String day) {
49+
if (day.equals("Sun")) return "Mon";
50+
if (day.equals("Mon")) return "Tue";
51+
if (day.equals("Tue")) return "Wed";
52+
if (day.equals("Wed")) return "Thu";
53+
if (day.equals("Thu")) return "Fri";
54+
if (day.equals("Fri")) return "Sat";
55+
if (day.equals("Sat")) return "Sun";
56+
return "";
57+
}
58+
}

finalVelocity3524.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import java.util.Scanner;
2+
public class finalVelocity3524 {
3+
public static void main(String[] args){
4+
Scanner in = new Scanner(System.in);
5+
System.out.println("2021503524 - Mugundh J B - 09/09/23 - 3 pm");
6+
7+
System.out.print("Enter the starting speed: ");
8+
float u = in.nextFloat();
9+
System.out.print("Enter the acceleration: ");
10+
float a = in.nextFloat();
11+
System.out.print("Enter the time: ");
12+
float t = in.nextFloat();
13+
14+
float d = (float) (u * t + (0.5 * a * t * t)); // Formula to calc displacement
15+
System.out.printf("The displacement is %.2f", d);
16+
17+
float v = u + a * t; // Formula to calc final velocity
18+
System.out.printf("\nThe final velocity is %.2f", v);
19+
20+
}
21+
}

headsTails3524.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import java.time.LocalDate;
2+
import java.time.LocalTime;
3+
import java.util.*;
4+
5+
public class headsTails3524 {
6+
public static void main(String[] args) {
7+
System.out.println("2021503524 - Mugundh J B");
8+
System.out.println("Date: " + LocalDate.now() + " Time: " + LocalTime.now());
9+
10+
// Create a random number generator
11+
Random rand = new Random();
12+
13+
// Generate a random number (0 or 1)
14+
int num = rand.nextInt(2);
15+
16+
// Create a Scanner to read user input
17+
Scanner in = new Scanner(System.in);
18+
19+
// Prompt the user for their choice
20+
System.out.print("Enter your choice (h / H / t / T): ");
21+
char choice = in.next().charAt(0);
22+
23+
// Validate user's choice
24+
while (choice != 'h' && choice != 'H' && choice != 't' && choice != 'T') {
25+
System.out.printf("Invalid choice. Enter your choice (h / H / t / T): ");
26+
choice = in.next().charAt(0);
27+
}
28+
29+
// Display the random value generated
30+
System.out.printf("Random value = %d\n", num);
31+
32+
// Check if the user won or lost based on their choice and the random value
33+
if (((choice == 'h' || choice == 'H') && num == 1) || ((choice == 't' || choice == 'T') && num == 0))
34+
System.out.print("You won!");
35+
else
36+
System.out.print("You lose!");
37+
38+
}
39+
}

helloWorld3524.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
import java.time.LocalDate;
3+
import java.time.LocalTime;
4+
5+
public class helloWorld3524 {
6+
public static void main(String[] a) {
7+
System.out.println("2021503524 - Mugundh J B");
8+
System.out.printf("Date: " + LocalDate.now() + " Time: " + LocalTime.now());
9+
System.out.print("\nHello, ");
10+
System.out.print(a[0]);
11+
System.out.println("\nGood morning! ");
12+
}
13+
}

monthDays3524.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import java.time.LocalDate;
2+
import java.time.LocalTime;
3+
import java.util.Scanner;
4+
5+
public class monthDays3524 {
6+
public static void main(String[] args) {
7+
System.out.println("2021503524 - Mugundh J B");
8+
System.out.println("Date: " + LocalDate.now() + " Time: " + LocalTime.now());
9+
Scanner scanner = new Scanner(System.in);
10+
11+
System.out.print("Enter the year: ");
12+
int year = scanner.nextInt(); // Getting the year as input
13+
14+
while (year < 1000 || year > 9999) { // Checking if the entered year is valid
15+
System.out.printf("Invalid input for the year!\nEnter the year again: ");
16+
year = scanner.nextInt(); // Getting input again if input is invalid
17+
}
18+
System.out.print("Enter the month (1 to 12): ");
19+
int month = scanner.nextInt(); // Getting the month as input
20+
21+
int daysInMonth = 0; // Variable to store no of days in that month
22+
switch(month){
23+
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
24+
daysInMonth = 31;
25+
break;
26+
case 4: case 6: case 9: case 11:
27+
daysInMonth = 30;
28+
break;
29+
case 2:
30+
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
31+
daysInMonth = 29; // Leap year
32+
} else {
33+
daysInMonth = 28; // Non-leap year
34+
}
35+
break;
36+
default:
37+
System.out.println("Invalid month!");
38+
return;
39+
}
40+
System.out.printf("Number of days in %d th month: %d", month, daysInMonth);
41+
}
42+
}

studentGrade3524.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import java.time.LocalDate;
2+
import java.time.LocalTime;
3+
import java.util.*;
4+
5+
public class studentGrade3524 {
6+
public static void main(String[] args) {
7+
System.out.println("2021503524 - Mugundh J B");
8+
System.out.println("Date: " + LocalDate.now() + " Time: " + LocalTime.now());
9+
// Create a Scanner to read user input
10+
Scanner in = new Scanner(System.in);
11+
// Prompt the user for the number of subjects
12+
System.out.print("Enter no of subjects: ");
13+
int n = in.nextInt();
14+
// Consume newline character left in buffer
15+
String buff = in.nextLine();
16+
// Initialize an array to store marks for each subject
17+
int[] arr = new int[n];
18+
// Prompt the user to enter marks for each subject and validate input
19+
System.out.println("Enter the marks: ");
20+
for (int i = 0; i < n; i++) {
21+
arr[i] = in.nextInt();
22+
if (arr[i] < 0 || arr[i] > 100) {
23+
System.out.println("Invalid input! Enter again: ");
24+
arr[i] = in.nextInt();
25+
}
26+
}
27+
// Determine the grade for each subject based on marks and display it
28+
for (int i = 0; i < n; i++) {
29+
if (arr[i] >= 90 && arr[i] <= 100)
30+
System.out.printf("Grade of subject %d = %s\n", i + 1, "O");
31+
else if (arr[i] >= 80 && arr[i] <= 89)
32+
System.out.printf("Grade of subject %d = %s\n", i + 1, "A+");
33+
else if (arr[i] >= 70 && arr[i] <= 79)
34+
System.out.printf("Grade of subject %d = %s\n", i + 1, "A");
35+
else if (arr[i] >= 60 && arr[i] <= 69)
36+
System.out.printf("Grade of subject %d = %s\n", i + 1, "B+");
37+
else if (arr[i] >= 50 && arr[i] <= 59)
38+
System.out.printf("Grade of subject %d = %s\n", i + 1, "B");
39+
else
40+
System.out.printf("Grade of subject %d = %s", i + 1, "U");
41+
}
42+
}
43+
}

0 commit comments

Comments
 (0)