Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added q1/q1.class
Binary file not shown.
21 changes: 18 additions & 3 deletions q1/q1.java
Original file line number Diff line number Diff line change
@@ -1,47 +1,62 @@
// FILL HERE: Import the necessary package for input operations

import java.util.Scanner;

// FILL HERE: Write the class declaration with proper naming convention
{
public class q1 {
// FILL HERE: Write the main method signature
{
public static void main(String[] args) {
// Variable declarations and initialization
// FILL HERE: Declare an integer variable named 'age' and initialize it to 25
int age = 25;

// FILL HERE: Declare a double variable named 'height' and initialize it to 5.8
double height = 5.8;

// FILL HERE: Declare a char variable named 'grade' and initialize it to 'A'
char grade = 'A';

// FILL HERE: Declare a boolean variable named 'isStudent' and initialize it to true
boolean isStudent = true;

// FILL HERE: Declare a String variable named 'name' and initialize it to "John Doe"
String name = "John Doe";


// Output statements
System.out.println("=== Student Information ===");

// FILL HERE: Print the name using System.out.println
System.out.println("Name: " + name);

// FILL HERE: Print the age using System.out.println (format: "Age: 25")
System.out.println("Age: " + age);

// FILL HERE: Print the height using System.out.println (format: "Height: 5.8 feet")
System.out.println("Height: " + height + " feet");

// FILL HERE: Print the grade using System.out.println (format: "Grade: A")
System.out.println("Grade: " + grade);

// FILL HERE: Print the student status using System.out.println (format: "Is Student: true")
System.out.println("Is Student: " + isStudent);


// Data type demonstration
System.out.println("\n=== Data Type Information ===");

// FILL HERE: Print the data type of age variable (hint: use "int")
System.out.println("age is of type: int");

// FILL HERE: Print the data type of height variable (hint: use "double")
System.out.println("height is of type: double");

// FILL HERE: Print the data type of grade variable (hint: use "char")
System.out.println("grade is of type: char");

// FILL HERE: Print the data type of isStudent variable (hint: use "boolean")
System.out.println("isStudent is of type: boolean");

// FILL HERE: Print the data type of name variable (hint: use "String")
System.out.println("name is of type: String");
}
}
Binary file added q2/q2.class
Binary file not shown.
75 changes: 63 additions & 12 deletions q2/q2.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,63 @@
// q2.java - Calculator Program
// Write a complete Java program that acts as a simple calculator

// Your program should:
// 1. Import the Scanner class
// 2. Create a class named 'q2'
// 3. Implement the main method
// 4. Get two numbers from the user
// 5. Perform arithmetic operations and display results
// 6. Demonstrate different types of operators (arithmetic, relational, logical)

// Write your complete solution below:
// q2.java - Simple Calculator Program

// Import the Scanner class for user input
import java.util.Scanner;

// Create a class named q2
public class q2 {
// Implement the main method
public static void main(String[] args) {
// Create a Scanner object for input
Scanner scanner = new Scanner(System.in);

// Prompt user for input
System.out.print("Enter first number: ");
double num1 = scanner.nextDouble();

System.out.print("Enter second number: ");
double num2 = scanner.nextDouble();

// Display header
System.out.println("\n=== Calculator Results ===");
System.out.println("First Number: " + num1);
System.out.println("Second Number: " + num2);

// Arithmetic operations
System.out.println("\n=== Arithmetic Operations ===");
System.out.println("Addition: " + num1 + " + " + num2 + " = " + (num1 + num2));
System.out.println("Subtraction: " + num1 + " - " + num2 + " = " + (num1 - num2));
System.out.println("Multiplication: " + num1 + " * " + num2 + " = " + (num1 * num2));

// Division with zero-check
if (num2 != 0) {
System.out.println("Division: " + num1 + " / " + num2 + " = " + (num1 / num2));
System.out.println("Modulus: " + (int)num1 + " % " + (int)num2 + " = " + ((int)num1 % (int)num2));
} else {
System.out.println("Division: Cannot divide by zero!");
System.out.println("Modulus: Cannot perform modulus by zero!");
}

// Relational operations
System.out.println("\n=== Relational Operations ===");
System.out.println(num1 + " > " + num2 + ": " + (num1 > num2));
System.out.println(num1 + " < " + num2 + ": " + (num1 < num2));
System.out.println(num1 + " >= " + num2 + ": " + (num1 >= num2));
System.out.println(num1 + " <= " + num2 + ": " + (num1 <= num2));
System.out.println(num1 + " == " + num2 + ": " + (num1 == num2));
System.out.println(num1 + " != " + num2 + ": " + (num1 != num2));

// Logical operations
System.out.println("\n=== Logical Operations ===");
boolean greater = (num1 > num2);
boolean notEqual = (num1 != num2);
boolean less = (num1 < num2);
boolean equal = (num1 == num2);

System.out.println("(" + num1 + " > " + num2 + ") && (" + num1 + " != " + num2 + "): " + (greater && notEqual));
System.out.println("(" + num1 + " < " + num2 + ") || (" + num1 + " == " + num2 + "): " + (less || equal));
System.out.println("!(" + num1 + " == " + num2 + "): " + (!equal));

// Close scanner
scanner.close();
}
}
Binary file added q3/q3.class
Binary file not shown.
89 changes: 40 additions & 49 deletions q3/q3.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,71 +8,71 @@ public static void main(String[] args) {
// Part 1: Different types of literals
System.out.println("=== Part 1: Literals Demo ===");

// FILL HERE: Create an integer literal variable 'decimal' with value 42
int decimal = 0; // Replace 0 with correct value
// Decimal literal variable 'decimal' with value 42
int decimal = 42;

// FILL HERE: Create a binary literal variable 'binary' with value 0b101010 (which is 42 in decimal)
int binary = 0; // Replace 0 with correct binary literal
// Binary literal variable 'binary' with value 0b101010 (42 in decimal)
int binary = 0b101010;

// FILL HERE: Create an octal literal variable 'octal' with value 052 (which is 42 in decimal)
int octal = 0; // Replace 0 with correct octal literal
// Octal literal variable 'octal' with value 052 (42 in decimal)
int octal = 052;

// FILL HERE: Create a hexadecimal literal variable 'hex' with value 0x2A (which is 42 in decimal)
int hex = 0; // Replace 0 with correct hexadecimal literal
// Hexadecimal literal variable 'hex' with value 0x2A (42 in decimal)
int hex = 0x2A;

System.out.println("Decimal literal: " + decimal);
System.out.println("Binary literal: " + binary);
System.out.println("Octal literal: " + octal);
System.out.println("Hexadecimal literal: " + hex);

// FILL HERE: Create a float literal variable 'floatNum' with value 3.14f
float floatNum = 0.0f; // Replace with correct value
// Float literal variable 'floatNum' with value 3.14f
float floatNum = 3.14f;

// FILL HERE: Create a double literal variable 'doubleNum' with value 2.718281828
double doubleNum = 0.0; // Replace with correct value
// Double literal variable 'doubleNum' with value 2.718281828
double doubleNum = 2.718281828;

System.out.println("Float literal: " + floatNum);
System.out.println("Double literal: " + doubleNum);

// FILL HERE: Create a char literal variable 'letter' with value 'A'
char letter = ' '; // Replace with correct character
// Character literal variable 'letter' with value 'A'
char letter = 'A';

// FILL HERE: Create a char literal variable 'unicodeChar' with value '\u0041' (which is 'A')
char unicodeChar = ' '; // Replace with correct unicode character
// Unicode character '\u0041' (which is 'A')
char unicodeChar = '\u0041';

System.out.println("Character literal: " + letter);
System.out.println("Unicode character: " + unicodeChar);

// FILL HERE: Create a boolean literal variable 'isTrue' with value true
boolean isTrue = false; // Replace with correct value
// Boolean literal variable 'isTrue' with value true
boolean isTrue = true;

// FILL HERE: Create a String literal variable 'message' with value "Hello, Java!"
String message = ""; // Replace with correct string
// String literal variable 'message' with value "Hello, Java!"
String message = "Hello, Java!";

System.out.println("Boolean literal: " + isTrue);
System.out.println("String literal: " + message);

System.out.println("\n=== Part 2: Increment/Decrement Operators ===");

// FILL HERE: Create an integer variable 'counter' with initial value 10
int counter = 0; // Replace with correct initial value
// Integer variable 'counter' with initial value 10
int counter = 10;

System.out.println("Initial counter value: " + counter);

// FILL HERE: Use post-increment (counter++) in the println statement
System.out.println("Post-increment (counter++): " + counter); // Add post-increment operation
// Post-increment (counter++)
System.out.println("Post-increment (counter++): " + counter++);
System.out.println("Counter after post-increment: " + counter);

// FILL HERE: Use pre-increment (++counter) in the println statement
System.out.println("Pre-increment (++counter): " + counter); // Add pre-increment operation
// Pre-increment (++counter)
System.out.println("Pre-increment (++counter): " + ++counter);
System.out.println("Counter after pre-increment: " + counter);

// FILL HERE: Use post-decrement (counter--) in the println statement
System.out.println("Post-decrement (counter--): " + counter); // Add post-decrement operation
// Post-decrement (counter--)
System.out.println("Post-decrement (counter--): " + counter--);
System.out.println("Counter after post-decrement: " + counter);

// FILL HERE: Use pre-decrement (--counter) in the println statement
System.out.println("Pre-decrement (--counter): " + counter); // Add pre-decrement operation
// Pre-decrement (--counter)
System.out.println("Pre-decrement (--counter): " + --counter);
System.out.println("Counter after pre-decrement: " + counter);

System.out.println("\n=== Part 3: Data Type of Expressions ===");
Expand All @@ -84,43 +84,34 @@ public static void main(String[] args) {
char charVar = 'B';

// Expression 1: int + int
// FILL HERE: Create a variable 'result1' that stores intVar + 3 (determine the correct data type)
int result1 = 0; // Replace with correct expression and fix data type if needed

int result1 = intVar + 3;
System.out.println("int + int = " + result1 + " (Type: int)");

// Expression 2: int + double
// FILL HERE: Create a variable 'result2' that stores intVar + doubleVar (determine the correct data type)
double result2 = 0.0; // Replace with correct expression and fix data type if needed

// Expression 2: int + double
double result2 = intVar + doubleVar;
System.out.println("int + double = " + result2 + " (Type: double)");

// Expression 3: float + double
// FILL HERE: Create a variable 'result3' that stores floatVar + doubleVar (determine the correct data type)
double result3 = 0.0; // Replace with correct expression and fix data type if needed

double result3 = floatVar + doubleVar;
System.out.println("float + double = " + result3 + " (Type: double)");

// Expression 4: char + int
// FILL HERE: Create a variable 'result4' that stores charVar + intVar (determine the correct data type)
int result4 = 0; // Replace with correct expression and fix data type if needed

int result4 = charVar + intVar;
System.out.println("char + int = " + result4 + " (Type: int, 'B' has ASCII value 66)");

System.out.println("\n=== Part 4: Operator Associativity ===");

// Demonstrate left-to-right associativity with subtraction
int a = 20, b = 10, c = 5;
// FILL HERE: Calculate result5 = a - b - c (should be evaluated as (a - b) - c)
int result5 = 0; // Replace with correct expression

int result5 = a - b - c;
System.out.println("Left-to-right: " + a + " - " + b + " - " + c + " = " + result5);
System.out.println("Evaluation: (" + a + " - " + b + ") - " + c + " = " + (a - b) + " - " + c + " = " + result5);

// FILL HERE: Demonstrate right-to-left associativity with assignment
int x = 0, y = 0, z = 0; // Replace this line with chained assignment x = y = z = 15
// Demonstrate right-to-left associativity with assignment
int x, y, z;
x = y = z = 15;

System.out.println("Right-to-left assignment: x = y = z = 15");
System.out.println("x = " + x + ", y = " + y + ", z = " + z);
}
}
}