0% found this document useful (0 votes)
4 views10 pages

Java Class8 ICSE Notes

Java Class8 Programming Solutions

Uploaded by

madhubelakoba
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views10 pages

Java Class8 ICSE Notes

Java Class8 Programming Solutions

Uploaded by

madhubelakoba
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

JAVA PROGRAMMING

Notes & Practice Problems for Beginners


Class 8 · ICSE Board · Computer Applications

This resource introduces Java programming fundamentals to Class 8 ICSE students who are writing their very
first programs. It covers core theory in simple language, worked examples, and a graded set of practice problems
(objective, short-answer, and programming) suitable for classwork, homework, or a class test.

Contents
1. Introduction to Java
2. Features of Java
3. Structure of a Java Program
4. Data Types in Java
5. Variables and Naming Rules
6. Operators in Java
7. Taking Input using the Scanner Class
8. Worked Example Programs
9. Practice Problems
[Link] / Hints
1. Introduction to Java

Java is a high-level, object-oriented programming language developed by James Gosling and his team at Sun Microsystems
in 1995. It is now owned and maintained by Oracle Corporation.
Java programs are platform-independent. This means a Java program written and compiled on one type of computer can run
on any other computer that has Java installed, without changing the code. This is possible because of the Java Virtual
Machine (JVM).
How Java works, in short:
• Source code is written in a file with extension .java
• The compiler (javac) converts the source code into bytecode (.class file)
• The JVM reads the bytecode and executes it on the actual machine

Note: In ICSE Class 8/9/10, Java programs are usually written and run using BlueJ, an easy-to-use Integrated Development
Environment (IDE) designed for beginners.

2. Features of Java

• Simple – Java syntax is easy to learn, especially for those who know basic programming concepts.
• Object-Oriented – Everything in Java is organised around objects and classes.
• Platform Independent – 'Write Once, Run Anywhere' (WORA), made possible by the JVM.
• Secure – Java has no explicit pointers and runs programs inside a virtual machine, making it safer.
• Robust – Java checks code both at compile time and run time, catching many errors early.
• Case Sensitive – Java treats uppercase and lowercase letters as different (e.g., Sum and sum are different identifiers).

3. Structure of a Java Program

Every Java program has a basic structure that must be followed. Here is a simple example:

class FirstProgram
{
public static void main(String args[])
{
[Link]("Hello, Class 8!");
}
}

Explanation of each part:


• class FirstProgram — declares a class named FirstProgram. The file must be saved as [Link] (same name as
the class, with matching capitalisation).
• public static void main(String args[]) — the main method. Every Java application must have exactly one main method;
this is where execution begins.
• [Link](...) — prints the given text to the screen and moves the cursor to the next line.
• Curly braces { } — mark the beginning and end of a class or a method.
• Semicolon ; — every statement in Java must end with a semicolon.

4. Data Types in Java

A data type tells the compiler what kind of value a variable will hold. Java's basic (primitive) data types are:

Data Type Used to Store

int Whole numbers, e.g. 10, -25, 1000

long Very large whole numbers

float Decimal numbers with less precision, e.g. 3.14f

double Decimal numbers with more precision, e.g. 3.14159

char A single character, e.g. 'A', '9', '$'

boolean Only two values: true or false

String A sequence of characters (a word or sentence), e.g. "Hello"

Note: char values are written in single quotes ('A'), while String values are written in double quotes ("Hello"). String is not a
primitive type — it is a class — but beginners use it just like a data type.

5. Variables and Naming Rules

A variable is a named location in memory used to store a value that can change during program execution.
Declaring and initialising a variable:

int age; // declaration


age = 13; // initialisation

int marks = 95; // declaration + initialisation together


double price = 49.50;
char grade = 'A';
boolean isPresent = true;

Rules for naming variables (identifiers):


• Must begin with a letter, underscore (_) or dollar sign ($) — never with a digit.
• Can contain letters, digits, underscore and dollar sign after the first character.
• Cannot be a Java reserved keyword (e.g. class, int, void, static).
• Are case-sensitive: Total and total are different variables.
• Cannot contain spaces or special symbols like @, #, %, etc.
• By convention, variable names start with a lowercase letter and use camelCase for multi-word names, e.g.
studentName, totalMarks.
6. Operators in Java

Arithmetic Operators

Operator Meaning

+ Addition

- Subtraction

* Multiplication

/ Division (quotient)

% Modulus (remainder)

Relational Operators (compare two values, give a boolean result)

Operator Meaning

== Equal to

!= Not equal to

> Greater than

< Less than

>= Greater than or equal to

<= Less than or equal to

Logical Operators

Operator Meaning

&& Logical AND — true only if both conditions are true

|| Logical OR — true if at least one condition is true

! Logical NOT — reverses the result

7. Taking Input using the Scanner Class

To accept input from the user (keyboard), Java provides the Scanner class, found in the [Link] package.

import [Link];

class InputDemo
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
[Link]("Enter your age: ");
int age = [Link]();
[Link]("Your age is " + age);
}
}

Common Scanner methods:

Method Reads a value of type

nextInt() int

nextLong() long

nextFloat() float

nextDouble() double

next() String (single word, stops at space)

nextLine() String (full line, including spaces)


8. Worked Example Programs

Example 1: Add two numbers entered by the user

import [Link];

class AddTwoNumbers
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
[Link]("Enter first number: ");
int a = [Link]();
[Link]("Enter second number: ");
int b = [Link]();
int sum = a + b;
[Link]("The sum is: " + sum);
}
}

Example 2: Find the area of a rectangle

import [Link];

class AreaOfRectangle
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
[Link]("Enter length: ");
double length = [Link]();
[Link]("Enter breadth: ");
double breadth = [Link]();
double area = length * breadth;
[Link]("Area of rectangle = " + area);
}
}

Example 3: Check whether a number is even or odd (using if-else)

import [Link];

class EvenOdd
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int num = [Link]();
if (num % 2 == 0)
[Link](num + " is Even");
else
[Link](num + " is Odd");
}
}
9. Practice Problems

A. Fill in the Blanks


1. Java programs are compiled into __________ code by the compiler.
2. The __________ class is used to accept input from the keyboard in Java.
3. Every statement in Java must end with a __________.
4. The data type used to store a single character is __________.
5. The __________ method of the Scanner class is used to read a whole line of text.
6. A Java program execution always begins from the __________ method.

B. State True or False


(a) Java is a platform-dependent language.
(b) Variable names in Java can begin with a digit.
(c) Java is case-sensitive.
(d) The % operator gives the remainder after division.
(e) A char value is written using double quotes.

C. Short Answer Questions


7. What is the Java Virtual Machine (JVM)? Why is it important?
8. List any three features of Java.
9. What is the difference between a char and a String in Java? Give one example of each.
[Link] any four rules for naming a variable in Java.
[Link] between the following pairs: (a) / and % operators (b) == and =
[Link] is the purpose of the import [Link]; statement?

D. Predict the Output


Find the output of the following code fragments:

int a = 15, b = 4;
[Link](a / b);
[Link](a % b);

int x = 5;
int y = 10;
[Link](x > y);
[Link](x < y && y > 0);

String name = "Rohan";


int age = 13;
[Link](name + " is " + age + " years old.");

E. Programming Problems
Write complete Java programs (with the class, main method, and Scanner where needed) for the following:
[Link] two numbers from the user and display their sum, difference, product, and quotient.
[Link] the radius of a circle and calculate its area and circumference. (Use 3.14 for π)
[Link] the length, breadth, and height of a cuboid and find its volume.
[Link] marks in three subjects and calculate and print the total and percentage (out of 300).
[Link] a number and print whether it is positive, negative, or zero.
[Link] the principal, rate, and time and calculate the Simple Interest using the formula SI = (P × R × T) / 100.
[Link] temperature in Celsius and convert it to Fahrenheit using F = (C × 9/5) + 32.
[Link] a student's name and age, then display a message in the format: "<name> will turn 18 in <n> years."
10. Answers / Hints

A. Fill in the Blanks


• 1. bytecode 2. Scanner 3. semicolon (;) 4. char 5. nextLine() 6. main

B. True or False
• (a) False — Java is platform-independent
• (b) False — a variable name cannot begin with a digit
• (c) True
• (d) True
• (e) False — char values use single quotes; double quotes are for String

D. Predict the Output


• Fragment 1: 3 followed by 3 (15/4 = 3, 15%4 = 3)
• Fragment 2: false followed by true
• Fragment 3: Rohan is 13 years old.

E. Programming Problems — Hints


• Q1: Use int/double variables and the +, -, *, / operators; declare two Scanner-read variables a and b.
• Q2: Area = π × r × r; Circumference = 2 × π × r.
• Q3: Volume = length × breadth × height.
• Q4: Total = sum of the three marks; Percentage = (Total / 300) × 100.
• Q5: Use if / else if / else with the conditions num > 0, num < 0, and num == 0.
• Q6: Apply the SI formula directly using double variables for P, R, T.
• Q7: Apply the formula directly; remember 9/5 should be written as 9.0/5 to avoid integer division.
• Q8: Use nextInt() for age and nextLine()/next() for name, then use String concatenation with +.

End of Notes — for classroom use.

You might also like