Make a lecture like this from your own PDF
Drop a PDF, slides or notes here. You'll sign up, then yours starts.
0:00 / 3:56
Shared by Luyanda Vilanculos
0:00To interact with a Java program, we use standard input and output. Think of 'System dot out' as the computer's voice—it displays results on your monitor. 'System dot in' refers to your keyboard. To actually read what a user types, we use a tool called a 'Scanner'. We create it by writing 'Scanner input equals new Scanner System dot in'. This line sets up a 'Scanner' object that watches the keyboard. When the user types a number and hits enter, the Scanner captures it so your program can use it for calculations.
0:35Variables are like named boxes in the computer's memory. Before using one, you must 'declare' it. This tells the computer the variable's name and what kind of data it will hold, like an 'int' for whole numbers or a 'double' for decimals. For example, 'int mark' reserves space for an integer. You can also 'initialize' it immediately by giving it a value, like 'int mark equals sixty-five'. If you don't initialize it, the box is created but it's empty, and you'll need to put something in it before you can use it.
1:09While variables can change, 'constants' stay the same forever once they are set. In Java, we use the 'final' keyword, like 'final double PI equals three point one four'. To keep code clean, we use specific naming styles. Variables and methods use 'camelCase'—where the first word is lowercase and others start with a capital, like 'showStudentMark'. Classes use 'PascalCase', starting with a capital letter, like 'ComputeArea'. Constants are special; they are written in ALL CAPS, making them very easy to spot in your code.
1:48Often, you want to update a variable based on its current value, like adding ten to a score. Instead of the long way, 'mark equals mark plus ten', Java gives us augmented operators. You can simply write 'mark plus-equals ten'. This works for all basic math: 'minus-equals' for subtraction, 'times-equals' for multiplication, and even 'remainder-equals'. These shortcuts don't change how the computer does the math; they just make your code shorter and much easier for other programmers to read quickly.
2:21To change a value by exactly one, we use 'plus-plus' or 'minus-minus'. But where you put the symbols matters. If you put them 'before' the variable, like 'plus-plus i', it's called a pre-increment. Java increases the value 'before' using it in that line. If you put them 'after', like 'i plus-plus', it's a post-increment. Java uses the 'original' value for the current line first, and 'then' adds one. It is a subtle but important difference when you are writing complex logic or loops.
2:55Let's put it all together with a program that calculates the area of a triangle. We start by importing the Scanner class. Inside our main method, we declare 'double' variables for base, height, and area. We then use 'System dot out' to ask the user for input and use 'input dot nextDouble' to save those numbers. The program calculates the area using 'base times zero point five times height'. Finally, we print the result. This 'Input, Process, Output' pattern is the heart of most software development.
3:30You've covered the essentials: reading user input with Scanners, storing data in variables and constants, following Java's naming style, and using shorthand operators to keep your code clean. These are the building blocks you'll use in every project from here on out. Take a moment to practice writing a few lines of code using these operators, and you'll be a Java pro in no time. Happy coding!