MIS 210: Introduction to Computer Programming
Lecture Manual -2
Introduction to Java Programming
(User Input/ Output and Conditional Statement)
1. Introduction
In this lecture, we will cover the following topics:
- Taking user input using `Scanner`
- Displaying output using `System.out.println()`
- Understanding conditional statements (`if`, `if-else`, `else-if`, `switch`)
- Writing programs that make decisions based on user input
2. Input and Output in Java
2.1 Output in Java
Java provides several ways to display output. The most common way is:
System.out.println("Hello, World!"); // Prints with a newline
System.out.print("Hello, World!"); // Prints without a newline
Example 1: Displaying Text Output
public class OutputExample {
public static void main(String[] args) {
System.out.println("Welcome to Java Programming!");
System.out.print("This is an example of output.");
}
}
2.2 Taking User Input in Java
To take input from the user, we use the `Scanner` class. The `Scanner` class is part of the
`java.util` package, which provides utility classes for various functionalities. To use it, we must
import `java.util.Scanner` at the beginning of our Java program.
import java.util.Scanner;
This import statement tells Java to include the `Scanner` class, allowing us to create `Scanner`
objects for user input.
MIS 210: Introduction to Computer Programming
Example 2: Taking Integer and String Input
import java.util.Scanner;
public class InputExample {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = scanner.nextLine();
System.out.print("Enter your age: ");
int age = scanner.nextInt();
System.out.println("Hello, " + name + "! You are " + age + " years old.");
}
}
Method Description
nextInt() Reads an integer
nextDouble() Reads a double value
nextLine() Reads a full line of text
next() Reads a single word
MIS 210: Introduction to Computer Programming
3. Conditional Statements in Java
3.1 if Statement
The `if` statement allows a program to execute code only if a condition is true.
Example 3: Checking if a Number is Positive
import java.util.Scanner;
public class IfExample {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scanner.nextInt();
if (number > 0) {
System.out.println("The number is positive.");
}
}
}
3.2 if-else Statement
Used when there are two possible outcomes.
Example 4: Checking Even or Odd Number
import java.util.Scanner;
public class EvenOddChecker {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scanner.nextInt();
if (number % 2 == 0) {
System.out.println("The number is even.");
} else {
System.out.println("The number is odd.");
}
}
}
MIS 210: Introduction to Computer Programming
3.3 else-if Ladder
Used when there are multiple conditions to check.
Example 5: Grading System
import java.util.Scanner;
public class GradingSystem {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter your marks: ");
int marks = scanner.nextInt();
if (marks >= 90) {
System.out.println("Grade: A+");
} else if (marks >= 80) {
System.out.println("Grade: A");
} else if (marks >= 70) {
System.out.println("Grade: B");
} else if (marks >= 60) {
System.out.println("Grade: C");
} else {
System.out.println("Grade: F (Fail)");
}
}
}
MIS 210: Introduction to Computer Programming
3.4 switch Statement
The switch statement allows a variable to be tested for equality against multiple values.
Example 6: Day of the Week
int day = 4;
switch (day) {
import
case java.util.Scanner;
1:
System.out.println("Monday");
break;
case 2:
public class SwitchExample {
System.out.println("Tuesday");
break;
public static void main(String[] args) {
case 3:
System.out.println("Wednesday");
Scanner scanner = new Scanner(System.in);
break;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
case 6:
System.out.println("Saturday");
break;
case 7:
System.out.println("Sunday");
break;
}
// Outputs "Thursday" (day 4)
Class work:
1. Create a program that takes a user's age as input and determines whether they are eligible
to vote (18+ years old).
2. Write a program that calculates the final price of an item after applying a discount based on
the following conditions:
a. If the price is greater than 1000, apply a 10% discount.
b. If the price is between 500 and 1000, apply a 5% discount.
c. Otherwise, no discount.
3. Develop a simple calculator program that takes two numbers and an operator (+, -, *, /) as
input and performs the respective operation.
a. Also include complex operations like applying the BODMAS rule