0% found this document useful (0 votes)
7 views12 pages

Java - Assignment 1 - Google Docs

The document contains Java programs that demonstrate various concepts, including checking for leap years, adding binary numbers, and displaying star patterns (right-aligned and diamond shape). Each program is accompanied by explanations of input handling and output formatting. The content serves as an introduction to Java programming through practical examples.

Uploaded by

hindunation69
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)
7 views12 pages

Java - Assignment 1 - Google Docs

The document contains Java programs that demonstrate various concepts, including checking for leap years, adding binary numbers, and displaying star patterns (right-aligned and diamond shape). Each program is accompanied by explanations of input handling and output formatting. The content serves as an introduction to Java programming through practical examples.

Uploaded by

hindunation69
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
You are on page 1/ 12

‭Introduction to Java - Flowcharts & Screenshots of Answers‬

‭Assignment 1:‬

// Program 1: Determine if a Given Year is a Leap Year‬


import java.util.Scanner;‬

public class BinarySum {‬


public static void main(String[] args) {‬


Scanner input = new Scanner(System.in); // takes input as input from‬



User in terminal‬

System.out.print("Enter a year: ");‬


int givenYear = input.nextInt();‬


input.close();‬

// Check if the year satisfies leap year conditions‬


if ((givenYear % 4 == 0 && givenYear % 100 != 0) || (givenYear % 400‬



== 0)) {‬

System.out.println(givenYear + " is a Leap Year.");‬


} else {‬

System.out.println(givenYear + " is not a Leap Year.");‬


}‬

}‬

}‬

‭Introduction to Java - Flowcharts & Screenshots of Answers‬

// Program 2: Binary Number Addition‬


import java.util.Scanner; // re imported as rest of code needed to be‬



commented out to run the class‬

public class BinarySum {‬


public static void main(String[] args) {‬


Scanner input = new Scanner(System.in);‬


System.out.print("Enter first binary number: "); // take input of‬



first binary number‬

String binNum1 = input.next();‬


System.out.print("Enter second binary number: "); // take input of‬



second one‬

String binNum2 = input.next();‬


input.close();‬

// Convert binary to decimal, perform the addition, then convert the‬



result back to binary‬

int decimal1 = Integer.parseInt(binNum1, 2);‬


int decimal2 = Integer.parseInt(binNum2, 2);‬


int sumResult = decimal1 + decimal2;‬


String binaryResult = Integer.toBinaryString(sumResult); // add the‬



result of both and then cast to a string to be printed‬

System.out.println("Sum: " + binaryResult);‬


}‬

}‬

‭Introduction to Java - Flowcharts & Screenshots of Answers‬

// Program 3: Display Right-Aligned Star Pattern‬


public class RightAlignedPattern {‬


public static void main(String[] args) {‬


int totalRows = 5; // Define the number of rows‬


for (int i = 1; i <= totalRows; i++) {‬


for (int j = 1; j <= totalRows - i; j++) {‬


System.out.print(" "); // Output spaces for alignment‬


}‬

for (int k = 1; k <= i; k++) {‬


System.out.print("*"); // Print star symbols‬


}‬

System.out.println();‬

}‬

}‬

}‬

‭Introduction to Java - Flowcharts & Screenshots of Answers‬

// Program 4: Display Diamond Shape Pattern‬


import java.util.Scanner;‬

public class DiamondShape {‬


public static void main(String[] args) {‬


Scanner input = new Scanner(System.in); // take input from User‬


System.out.print("Enter number of rows: "); // ask user for no. of‬



rows of *‬

int totalRows = input.nextInt();‬


input.close();‬

// Create the upper part of the diamond‬


for (int i = 1; i <= totalRows; i += 2) {‬


for (int j = 0; j < (totalRows - i) / 2; j++) {‬


System.out.print(" ");‬

}‬

for (int k = 0; k < i; k++) {‬


System.out.print("*");‬

}‬

System.out.println();‬

}‬

‭Introduction to Java - Flowcharts & Screenshots of Answers‬

// Create the lower part of the diamond‬


for (int i = totalRows - 2; i > 0; i -= 2) {‬


for (int j = 0; j < (totalRows - i) / 2; j++) {‬


System.out.print(" ");‬

}‬

for (int k = 0; k < i; k++) {‬


System.out.print("*");‬

}‬

System.out.println();‬

}‬

}‬

}‬

‭1.‬ ‭Leap Year Checker‬


‭Introduction to Java - Flowcharts & Screenshots of Answers‬
‭Introduction to Java - Flowcharts & Screenshots of Answers‬

‭2.‬ ‭Binary Addition‬


‭Introduction to Java - Flowcharts & Screenshots of Answers‬

‭3.‬ ‭Right-Aligned Star Pattern‬


‭Introduction to Java - Flowcharts & Screenshots of Answers‬
‭Introduction to Java - Flowcharts & Screenshots of Answers‬
‭Introduction to Java - Flowcharts & Screenshots of Answers‬

‭4.‬ ‭Diamond Pattern‬


‭Introduction to Java - Flowcharts & Screenshots of Answers‬

You might also like