Java 2.2 - Chapter 2 notes PDF

Title Java 2.2 - Chapter 2 notes
Course Introduction to Scripting
Institution Southern New Hampshire University
Pages 2
File Size 79.3 KB
File Type PDF
Total Downloads 1
Total Views 123

Summary

Chapter 2 notes ...


Description

2.2 Writing a Simple Program  Writing a program involves designing algorithms and translating algorithms into programming instructions, or code  algorithm describes how a problem is solved by listing actions that need to be taken and the order of their execution o Can help the programmer plan a program before writing it in a programming language o Can be described in natural languages or in pseudocode (natural language mixed with some programming code) o Ex. 1. Read in the circle’s radius 2. Compute the area using the following formula area = radius x radius x pi(symbol) 3. Display the result  When you code is when you write a program o You translate an algorithm into a program  Every Java program must have a main method where program execution begins  Ex. class is ComputeArea public class ComputeArea { public static void main(String[] args) { // Step 1: Read in radius // Step 2: Compute area // Step 3: Display the area } } 

In order to store the radius, the program needs to declare a symbol called a variable o Represents a value stored in the computer’s memory o Rather than using x and y as variable name, choose descriptive names o Ex. radius for radius and area for area o Specify their data types (declaring variables) o Java provides simple data types for representing integers, floating-point numbers (i.e. numbers with a decimal point), characters, and Boolean types  These are known as primitive data types or fundamental types  Ex. class is ComputeArea public class ComputeArea { public static void main(String[] args) { double radius; double area; // Step 1: Read in radius // Step 2: Compute area // Step 3: Display the area

} }     

double indicates that radius and area are double-precision floating-point values stored in the computer You can assign a fixed value to variables Assign the results of the expression to a variable Display the value of the variable you computed on the console by using the System.out.println method Ex. class is ComputeArea public class ComputeArea { public static void main(String[] args) { double radius; // Declare radius double area; // Declare area // Assign a radius radius = 20; // radius is now 20 // Compute area area = radius * radius * 3.14159 // Display results System.out.println(“The area for the circle of radius” + radius + “is” + area); }

  

} Variables such as radius and area correspond to memory locations Every variable has a name, a type, a size, and a value Each row in the table shows the values of variables after the statement in the corresponding line in the program is executed o tracing a program o Helpful for understanding how programs work and finding errors  the plus sign has two meaning: one for addition and the other for combining strings  System.out.println(“The area for the circle of radius” + radius + “is” + area); → string concatenation operator o Combines two strings into one...


Similar Free PDFs