0% found this document useful (0 votes)
7 views8 pages

Java Scanner and Array Basics for Grade 11

The document provides notes on Java programming concepts for Grade 11, focusing on the Scanner method for user input, arrays for data storage, and exception handling with try-catch blocks. It also covers search algorithms like Selection Sort and Bubble Sort, as well as the Binary Search method, emphasizing the importance of sorted arrays for effective searching. Additionally, it includes tips for breaking down programming questions and pseudocode examples for problem-solving.

Uploaded by

yuvipillay2104
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)
7 views8 pages

Java Scanner and Array Basics for Grade 11

The document provides notes on Java programming concepts for Grade 11, focusing on the Scanner method for user input, arrays for data storage, and exception handling with try-catch blocks. It also covers search algorithms like Selection Sort and Bubble Sort, as well as the Binary Search method, emphasizing the importance of sorted arrays for effective searching. Additionally, it includes tips for breaking down programming questions and pseudocode examples for problem-solving.

Uploaded by

yuvipillay2104
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

Java Notes – Grade 11

1) SCANNER METHOD
What is it?

It’s a "tool" that lets our Java program read input from the user or a file.

Lets go over what each line of code does:

Code Line What does it do


import [Link]; Imports the Scanner class. So We're
telling Java, “Hey, I'm going to use that
Scanner tool in the toolbox, go get it
for me.”
Scanner myScanner = new Creates a Scanner object. "We're
Scanner([Link]); building the actual pair of ears and
giving it a name, myScanner.
[Link] means it will listen to the
keyboard."
String name = [Link](); Reads a full line of text. The program
stops and waits. Whatever the user
types until they press 'Enter' gets
stored in the name variable.
int age = [Link](); Reads a whole number. The program
stops and waits for a number. If the
user types text, the program crashes.
[Link](); Closes the scanner. We're done using
our ears, so we put them away. It's
good manners for the computer.
DateTimeFormatter f = This just takes the date of the textfile
[Link](“dd/MM/yyyy”) and turn it into this format e.g
12/02/2002
Scanner scLine = new This helps split the line of text into
Scanner(line).useDelimiter(“”) more readable, why is it like that in the
first place, just helps to see what each
word or number is being written so
instead of johnDoeSeden250 –
john,Doe,Seden,250.
String line = [Link](); This helps read one line from file
Why the sc in line or file?? Sc just means Scan/scanner.
Very important to note that every scanner needs to be within a try and catch as it is
trying to scan a file and will catch any errors within the file. Remember that you need
to take the file out of your resource into your project in netbeans so it has the file
there.
Lets go over a simple scanner method:

How the text file would like:


2) ARRAYS
What is an Array?

A container that can hold a fixed number of items of the same type.

What to remember about the array:

 The position number. In programming, we start counting at 0. The first egg is at


index 0.
 The fixed size of the array ([Link] would be 12).
 Initialization:
o int[] eggCartoon = new int[12]; (An empty carton for 12 marks)

lets go over How it would look like in code:

Public class EggsArray


{
Private EggsArray[] eArr = new EggsArray[12];
Private int size;
}

The array stores the eggs objects like 12 empty slots which will be filled by the textfi[Link]

Now how would you display? In grade11, you will only be displaying the textfile but think
of the array to temporally taking the array to hold the amount and arrange them.

Lets look at the programming once more with the egg method

Public EggsArray
{
Private EggsArray[] eArr = new EggsArray[12].
Private int size;

Public EggsArray
{
Try
{
Scanner scFile = new Scanner (new File(“[Link]”));
While ([Link]())
{
String line = [Link]();
Scanner scLine = [Link]();

Scanner scLine – new Scanner(line).useDelimiter(“-”);


String egg = [Link]();
String name = [Link]();

eArr[size] = new Eggs(egg)


size++;
}
[Link]();
}
catch(FileNotFoundException e)
{
[Link](“The file was not found”);
}
}
}

The textFile would look like something like this:

3) Breaking down the questions


Must not think of what they are asking me but rather what are they asking me to do and
how do I approach this?

 Read the question. Then read it again.


 Underline the Nouns (the "What"): These are your data. (e.g., "an array of names",
"a text file", "a specific date"). This tells you what variables you need.
 Circle the Verbs (the "How"): These are your actions. (e.g., "read", "calculate",
"display", "search", "sort"). This tells you what methods and loops you need.
 Write the steps on paper(you should be allowed but only if you have time)
 Example: "Question: Find the oldest person in birth years in order."
 Pseudocode:
o Create an array of birth years.
o Create a variable oldestYear and set it to the first element.
o Loop through the array.
o If the current year is less than oldestYear, update oldestYear.
o After the loop, print oldestYear.

4) Try-Catch Method
Think of this method as Exception Handing, as the TRY Statement allows to define a
block of code to be tested for errors while it is executed. The CATCH Statement allows
to catch any errors that were being found in the TRY Statement. These two will always
come in pairs.

2023 IEB paper 1

So what is this part of code mean?

a SongManager class, It begins by attempting to open and read a file named "[Link]"
using a Scanner. Then processes the file line by line; for each line, it uses a
second Scanner with a period as a delimiter to parse the individual data components:
the song name, the artist, a date string, and a points value. The date string is then
converted into a formal LocalDate object using a specified date format. With these
extracted values, the code creates a new Song object, which is stored in an array
called sArr, and a counter variable size is incremented to keep track of the total number
of songs loaded. Finally, to handle the potential error of the file being missing, the entire
process is wrapped in a try-catch block that will print a "File not found!" error message if
the text file was not found.

4) Array Length & Typing


The property returns an integer value representing the number of elements in the array
lets look at a example:

length is a property, not a method. Therefore, you do not use parentheses after it
[Link](). Remember the length of an array is fixed once it is created and cannot be
changed.
5) Search Algorithms
In simple terms they are used to find a specific element within the data structure. Lets
look at the two that we’ll go over:

Selection Sort:

It’s simple algorithm, it works by repeatedly finding the minimum element from the
unsorted part of the array to the beginning of the sorted part.

Bubble Sort:

This is another simple method that most would prefer to use. As the method repeatedly
steps through the list, compares adjacent elements, and swaps them if they are in the
wrong order. The pass through the list is repeated until no swaps are needed, which
indicates that the list is sorted.
6) Binary Search:

Note it’s important to note that the array must be sorted first in order the Binary Search
be use so when it comes to the exam if they don’t ask you to search it then don’t.

Now the big question is when would we use this method? Think of it of finding a word in
a dictionary. You open to the middle of the dictionary and then keep dividing sections
until you find the word you are looking for.

You might also like