Java Assignment 2 - Scanner Class
Assignment 2
Java Scanner Class, Variables, Data Types & Type Casting
Student Name: ______________________ Date: ______________
Topic: Java Basics - Input and Type Conversion Total Questions: 15
Learning Goals
1. Take input from user using Scanner class.
2. Apply variables and correct Java data types.
3. Use type casting in practical programs.
4. Understand next(), nextLine(), and char input.
Scanner Methods Reference
Data Type Scanner Method Example
int nextInt() int age = [Link]();
double nextDouble() double salary = [Link]();
float nextFloat() float marks = [Link]();
long nextLong() long mobile = [Link]();
boolean nextBoolean() boolean passed = [Link]();
String - one word next() String name = [Link]();
String - full line nextLine() String address = [Link]();
char next().charAt(0) char grade = [Link]().charAt(0);
Practice Sheet | Variables, Data Types, Type Casting and User Input Page 1
Java Assignment 2 - Scanner Class
Starter Template
Use this structure for most programs in this assignment.
import [Link];
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
// write your code here
[Link]();
}
}
Coding Exercise Problems
Q1. Student Profile Printer
Take student name, age, class, and percentage as input. Print all details in a neat format.
Hint: Use String, int, and double.
Q2. Addition Calculator
Take two integer numbers from the user and print their sum.
Hint: Use nextInt().
Q3. Rectangle Area
Take length and breadth as input and calculate area of a rectangle.
Hint: Formula: area = length * breadth.
Q4. Circle Area
Take radius as input and calculate area of a circle.
Hint: Use double pi = 3.14.
Q5. Celsius to Fahrenheit
Take temperature in Celsius and convert it into Fahrenheit.
Hint: Formula: F = (C * 9 / 5) + 32.
Q6. Character ASCII Value
Take one character input from user and print its ASCII value.
Hint: Type cast char into int.
Practice Sheet | Variables, Data Types, Type Casting and User Input Page 2
Java Assignment 2 - Scanner Class
Q7. Double to Integer Casting
Take a double value from user and convert it into int using type casting. Print both values.
Hint: Example: 56.78 becomes 56.
Q8. Swap Two Numbers
Take two numbers and swap them using a third variable.
Hint: Print before and after swapping.
Practice Sheet | Variables, Data Types, Type Casting and User Input Page 3
Java Assignment 2 - Scanner Class
Coding Exercise Problems - Continued
Q9. Average Marks
Take marks of 5 subjects and calculate total and average percentage.
Hint: Use int for marks and double for average.
Q10. Simple Interest
Take principal, rate, and time as input and calculate simple interest.
Hint: Formula: SI = (P * R * T) / 100.
Q11. Full Name Builder
Take first name and last name separately and print full name.
Hint: Use next() for first and last name.
Q12. Int + Double Addition
Take one integer and one double input. Add them and print the result.
Hint: Observe automatic type conversion.
Q13. Seconds Converter
Take total seconds as input and convert it into minutes and remaining seconds.
Hint: Example: 125 seconds = 2 minutes 5 seconds.
Q14. Salary Increment
Take salary as input and increase it by 10 percent. Print original and updated salary.
Hint: Use double for salary.
Q15. next() vs nextLine()
Take one word using next() and one full sentence using nextLine(). Print both and observe
the difference.
Hint: Remember to handle buffer using [Link]().
Submission Checklist
Checklist Item Done
Practice Sheet | Variables, Data Types, Type Casting and User Input Page 4
Java Assignment 2 - Scanner Class
Code compiles without error [ ]
Scanner class used correctly [ ]
Meaningful variable names used [ ]
Output is formatted properly [ ]
All 15 programs completed [ ]
Practice Sheet | Variables, Data Types, Type Casting and User Input Page 5