0% found this document useful (0 votes)
9 views4 pages

Java Programming Basics and Examples

This document contains a lab sheet prepared by Krishna Kumari Ganga for an Advanced Programming 1 course in the Department of Computer Engineering at Omar Mukhtar University, Libya. The lab sheet includes 14 Java programs covering basic concepts like "Hello World", variable declaration and usage, operators, conditional statements, and switch statements. It provides examples and instructions to help students learn and practice core Java programming concepts.
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)
9 views4 pages

Java Programming Basics and Examples

This document contains a lab sheet prepared by Krishna Kumari Ganga for an Advanced Programming 1 course in the Department of Computer Engineering at Omar Mukhtar University, Libya. The lab sheet includes 14 Java programs covering basic concepts like "Hello World", variable declaration and usage, operators, conditional statements, and switch statements. It provides examples and instructions to help students learn and practice core Java programming concepts.
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

Omar Mukhtar University Faculty of Engineering

Dept. of Computer Engineering


Albeida , Libya
Instructor: Mrs. Krishna Kumari Ganga
Advanced Programming 1: Lab sheet I

1. Hello program in JAVA


class Hello{
public static void main(String arg[]) {
[Link]((“Hello World!”);
}
}

2. Hello program in JAVA using Single line comments


class Hello1
//Hello Students
//Good Morning
//Today we are going to execute our programs using JAVA
//This is our first program in JAVA {
public static void main(String arg[]){
[Link]((“Hello World!”);
}
}
3. Hello program in JAVA using multiple line comments
class Hello2 {
/*
Hello Students
Good Morning
Today we are going to execute our programs using JAVA
This is our first program in JAVA
*/
public static void main(String arg[]) {
[Link]((“Hello World!”);
}
}
4. JAVA program to add two numbers
class AddNum {
public static void main(String arg[]){
[Link](3+5);
}
}
5. JAVA program to add two numbers using variables of type integer
class AddInt {
public static void main(String arg[]) {
int a=3;
int b=5;
[Link](a+b);
}
}
6. program to add two numbers using variables
class AddInt1 {
public static void main(String arg[]) {
int a=3;
int b=5;
a=8;
[Link](a+b);
}
}

Advanced Programming Using Java - Prepared by: Krishna Kumari Ganga


7. program to add two numbers using variables of type float
class AddFloat {
public static void main(String arg[]) {
float a=3.6;
float b=5.3;
[Link](a+b);
}
}
Note:-By default any real number is double in java. So it generates an error.
8. program to add two numbers using variables of type double
class AddDouble {
public static void main(String arg[]) {
double a=3.6;
double b=5.3;
[Link](a+b);
}
}
Since double takes more memory space we can use float for normal real numbers by keeping ‘f ‘after the
value
9. program to add two numbers using variables of type float
class AddFloat1 {
public static void main(String arg[]) {
float a=3.6f;
float b=5.3f;
[Link](a+b);
}
}
10. program using variables of type char
class CharDisp {
public static void main(String arg[]) {
Char c=’A’;
[Link](c);
}
}
11. program using type casting
class DemoCast {
public static void main(String arg[]) {
Char c=’A’;
[Link]((int)c);
}
}
12. program using type casting
class DemoCast1 {
public static void main(String arg[]) {
int num=’A’;
[Link]((char)num);
}
}
13. program using Unary operator(++, --)
class DemoUnary {
public static void main(String arg[]) {
int a=5;
[Link](a);
a++;
[Link](a);

int b=9;

Advanced Programming Using Java - Prepared by: Krishna Kumari Ganga


[Link](b);
b--;
[Link](b);
}
}
14. program using Unary operator(++, --)
class Conditional {
public static void main(String arg[]) {
int a=5;
int b=9;
int big = ( a > b ) ? a : b;
[Link]("The biggest number is " +big);
}
}

Conditional Statements
1. program to find whether the given number is odd or even
class OddEven {
public static void main(String arg[]) {
int num=5;
if (num%2==1) {
[Link](“The number is even”);
}
Else {
[Link](“The number is odd”);
}
}
}
2. program to find the biggest number
class Biggest {
public static void main(String arg[]) {
int num1=5;
int num2=9;
int num3=7;
if (num1>num2 && num1>num3) {
[Link](“Num1 is the biggest”);
}
else if (num2>num3) {
[Link](“Num2 is the biggest”);
}
else {
[Link](“Num3 is the biggest”);
}
}
}
3. program using Switch statement
class DemoSwitch {
public static void main(String arg[]) {
String s=”Hai”;
switch(s) {
case “Hai”:
[Link](“Hai”);
break;
case “Hello”:
[Link](“Hello”);

Advanced Programming Using Java - Prepared by: Krishna Kumari Ganga


break;
default:
[Link](“Welcome”);
}
}
}
4. program using Switch statement
class DemoSwitch1 {
public static void main(String arg[]) {
int i=5;
switch(i) {
case 1:
[Link](“January”);
break;
case 2:
[Link](“Febraury”);
break;
case 3:
[Link](“March”);
break;
case 4:
[Link](“April”);
break;
case 5:
[Link](“May”);
break;
case 6:
[Link](“June”);
break;
case 7:
[Link](“July”);
break;
case 8:
[Link](“August”);
break;
case 9:
[Link](“September”);
break;
case 10:
[Link](“October”);
break;
case 11:
[Link](“November”);
break;
case 12:
[Link](“December”);
break;
default:
[Link](“Not a valid number”);
}
}
}

Best of luck

Advanced Programming Using Java - Prepared by: Krishna Kumari Ganga

Common questions

Powered by AI

In Java, multiplication in integer arithmetic directly denotes the operation 'x * y', where each operand is evaluated and the product is computed as per mathematical multiplication. Conversely, unary operations like '++' and '--' act on a single operand, incrementing or decrementing its value by one respectively. Unary operations modify the operand itself, e.g., 'a++' directly changes 'a', while multiplication expressions evaluate and return a new product value without altering the original operands . The two represent different aspects of arithmetic - unary operators for incrementation and multiplication for scalable numeric transformations.

The use of single-line comments in Java, indicated by '//', allows for annotations and explanations within the code without affecting the program's execution. The Hello World program using single-line comments includes comments before the main method declaring messages such as "Hello Students", "Good Morning", etc. . In contrast, the multi-line comments, enclosed in '/* ... */', encompass an entire block of text which can span multiple lines, also providing explanations or context before the main method, such as "Hello Students Good Morning Today we are going to execute our programs using JAVA" . Both methods of commenting do not alter the program's functionality but enhance understandability for programmers.

Reassignment of integer variables causes initial values to be overwritten before any operations are executed. For instance, if the program initially declares 'int a = 3;' and later reassigns 'a = 8;', only the last assigned value (8) is used in subsequent arithmetic calculations . In an addition operation such as 'System.out.println(a + b);', replacing the initial value causes the output to reflect the new computation, demonstrating reassignments' effect on code predictability and outcomes. Understanding variable scoping and reassignment is crucial in accurately debugging and predicting Java program outputs.

The switch statement often provides a clearer and more structured method of handling multiple specific discrete values than if-else constructs. Each case in a switch block aligns syntactically for readability, reducing complexity compared to nested if-else where logical conditions must be evaluated sequentially until a match is found . Switch benefits include better performance with enhanced execution speed due to constant-time complexity for matching cases, unlike the linear complexity of typical if-else chains. Furthermore, switch statements prevent fall-through errors efficiently using 'break', an advantage over manually constructed if-else ladders, enhancing maintenance efficiency and clarity.

Float is preferable over double when memory efficiency is more critical than the precision of the stored numbers. A float occupies 4 bytes of memory compared to 8 bytes for a double, which can make a significant difference in applications where numerous floating-point calculations are performed, especially on devices with memory constraints . Moreover, for real numbers whose precision beyond 7 decimal digits is unnecessary, using float might suffice while saving memory space. Annotating floats with 'f', such as '3.6f', avoids errors caused by Java treating real numbers as doubles by default .

The conditional (ternary) operator provides a more concise way to express simple conditional logic compared to traditional if-else statements. When determining the maximum of two numbers, the ternary operator can condense the logic into a single line, as in: 'int big = (a > b) ? a : b;' This line directly assigns the greater value to 'big' based on the condition, improving readability and reducing code complexity . Meanwhile, using if-else requires more lines and is less succinct, although it can be clearer for complex conditions involving multiple steps or operations. However, the ternary operator is limited to more straightforward expressions and might reduce code clarity in complex scenarios.

The program demonstrated with unary operators starts by initializing 'a' to 5 and 'b' to 9. Firstly, it prints the initial value of 'a', then applies the increment operator '++', effectively changing 'a' to 6, and prints the updated value . Similarly, it prints the initial value of 'b', applies the decrement operator '--', changing 'b' to 8, and prints the updated result. The logical sequence involves reading the value, applying the unary operation, and displaying the results, illustrating basic increments and decrements in Java.

The 'default' case in a switch statement acts as a fallback mechanism, handling any input that does not match the predefined cases. Its inclusion ensures robustness by preventing undefined behavior or errors when unexpected inputs occur. For example, in a month-switching application, inputs outside the 1-12 range would fall through to 'default', outputting 'Not a valid number' . This safeguard helps maintain program stability and reliability, ensuring the user receives appropriate feedback or corrective prompts for invalid data entries, thereby enhancing user experience and reducing maintenance errors.

The switch statement in Java is used to map discrete input values to specific outputs, such as month names for integers 1 to 12. For example, using the input '5', the program navigates to 'case 5', outputs 'May' and exits the switch block with 'break'. If an integer outside the 1-12 range is given, the 'default' case is triggered, outputting 'Not a valid number', as demonstrated in the 'DemoSwitch1' program . This approach contrasts with using multiple if-else conditions and provides a clean, efficient way to handle multiple discrete cases.

Type casting is used to convert a character data type to an integer in Java by explicitly stating the desired type in parentheses before the value. For instance, casting a char 'A' to an int involves placing the character in parentheses with 'int', as shown in the program 'DemoCast': 'System.out.println((int)c);' where 'c' is 'A' . This conversion outputs the integer representation of 'A', which is 65 according to the ASCII character set. Type casting is useful for manipulating character data in numerically driven contexts.

You might also like