minurvaaa
minurvaaa
WISDOM'S CHILD.
3 posts
i know that i know nothing
Don't wanna be here? Send us removal request.
minurvaaa · 6 years ago
Text
CHAPTER THREE.
input & output
This chapter will go over reading input from the keyboard, use that input to reach an end, and then allow for the output to be displayed.
3.1 --- the system class
System is a keyword that refers to any methods that are related to where the program is run. Thus, System.out refers to output from the system & the methods for displaying that output, println & print. When System.out is printed ( java.io.PrintStream@685d72cd ), the address of System.out is shown and it is revealed to be a PrintStream. The numbers and letters that follow the @ symbol are the location of the system in the computer’s memory in hexadecimal form. Thus, it is different on different computers. These values are all part of the Java library.
3.2 --- the scanner class
The System class also grants access to System.in, an InputStream that makes it possible to read input from the keyboard. But this method is not an easy one. Instead, it would be wise to make use of the Scanner class. It allows for the input of words, numbers, and other forms of data, and is given in the java.util package, a package of “ utility classes ”. To use the Scanner class, it must be first imported at the beginning of the code by using import java.util.Scanner;. This import statement informs the computer that this is what the programmer means when they use the keyword Scanner.
To create a Scanner, the code Scanner name = new Scanner(System.in); must be used. This will create a Scanner variable and a new Scanner that will be able to be accessed by System.in. Scanner also provides a method called nextLine. This method reads a line of input from the keyboard and returns a String.
Without the importation of the java.util.Scanner package, an error will be thrown.
3.3 --- program structure
A package contains classes which contain methods which contain statements which contain expressions which contain tokens.
Packages can be found here.
3.4 --- inches to centimeters
Programs can be created to convert inches to centimeters & vice - versa by the use of a Scanner & expressions. This will be turned in in class.
3.5 --- literals & constants
Values that appear in programs that are constant are referred to as literals. If they appear without explanation as to why they are there, they are also known as magic numbers. This is not a good thing because it implies that the programmer came upon those numbers as if by magic. Thus, it is important to assign these kinds of numbers to variables with meaningful names. In order to ensure that these variables remain constant, like how many centimeters are in an inch, it is important to put the keyword final before them. Once that is done, these values cannot be reassigned unless the final keyword is removed.
3.6 --- formatting output
By replacing the print in System.out.print() with printf, one can format it to display less than the 16 decimal place maximum of a double number. The location of where this will be cut off can be designated by inserting “ %. ” followed by the number of decimal places that will be allowed. For example, System.out.printf(“Four thirds = %.3f”, 4.0 / 3.0 ); will generate 1.333.
Other format specifiers include:
%d - decimal integer - 12345 
%08d - padded with zeros, at least 8 digits wide - 00012345 
%f - floating-point - 6.789000 
%.2f - rounded to 2 decimal places - 6.79
3.7 --- centimeters to inches
Programs can be created to convert centimeters to inches & vice - versa by the use of a Scanner & expressions. However, if 2.54 is the number of centimeters in one inch, the integer type must first be cast onto the floating - point variable before these numbers can be divided or multiplied by each other. For example, if there are 5 centimeters and one wishes to divide them by 2.54 cm to determine the number of inches there are, this would not work unless both values were of the same data type.
3.8 --- modulus operator
In modulus division, one number is divided by another, but instead of the whole number being kept, only the remainder is stored in memory. The symbol used for this kind of division is a percent sign. For example, 76 / 12 in integer division is 6, but 76 % 12 is 4 in modulus division.
3.9 --- putting it all together
By importing Java library classes, creating a Scanner, inputting information from a keyboard, formatting that information, and dividing & / or modding it, programs can be written that can take out much of the legwork that some computations require.
3.10 --- the scanner bug
When running a Scanner, be sure to use the correct statements in the correct order or create additional statements so that certain ones are not skipped. If using the nextInt command, one that already calls a newline, before a line filled with just a String, that entire line will be skipped since it awaits the next numeric character or a newline escape sequence. In order to avoid this, another newline must be added so that it is read before the String, thus allowing the following String to be read. Alternatively, laying out the code differently, to begin with, can prevent this issue. Presenting the String before the int value will allow for the String to be displayed, then for the newline to be reached, then for the integer to be read.
3.11 --- vocabulary
package: A group of classes that are related to each other. 
address: The location of a value in computer memory, often represented as a hexadecimal integer.
library: A collection of packages and classes that are available for use in other programs. 
import statement: A statement that allows programs to use classes de ned in other packages. 
token: A basic element of a program, such as a word, space, symbol, or number. 
literal: A value that appears in source code. For example, "Hello" is a string literal and 74 is an integer literal. 
magic number: A number that appears without explanation as part of an expression. It should generally be replaced with a constant. 
constant: A variable, declared final, whose value cannot be changed. 
format string: A string passed to printf to specify the format of the output. 
format specifier: A special code that begins with a percent sign and specifies the data type and format of the corresponding value. 
typecast: An operation that explicitly converts one data type into another. In Java, it appears as a type name in parentheses, like (int). 
modulus: An operator that yields the remainder when one integer is divided by another. In Java, it is denoted with a percent sign; for example, 5 % 2 is 1.
0 notes
minurvaaa · 6 years ago
Text
CHAPTER TWO.
variables & operators
Variables, as will be described in this chapter, store values like numbers and words, and operators, symbols that will perform computations. 
2.1 --- declaring variables
According to the textbook, a variable is a named location that stores a value. These values can be text, images, sounds, numbers, and countless other kinds of data. In order for a value to be stored, the variable that it will belong to must be declared. The declaration of the variable is as simple as typing String message; and, later on, that variable can be called to fulfill a purpose. This declaration essentially says that there will be a String variable called message created in the system’s memory. Meaning, message will be able to store Strings. There are other types of variables that can store different kinds of data --- such as char, which can store characters, and int, which can store integers. To store other kinds of values, one must simply declare the type of value followed by the name and a semicolon. ( ex: int x; or char f; ). Any name can be used for a variable so long as it is not a reserved word in memory.
A complete list of keywords can be found here.
Multiple variables can be declared by stating the data type and separating each variable name with a comma.
2.2 --- assignment
Values can be assigned to these variables once they are declared by adding an equals sign after the name and placing a value within quotation marks ( ex: message = “ 123 ” ). However, a variable must have already been initialized for a value to be assigned. Another option is to declare / initialize a variable and then immediately assign a value ( ex: int message = “ 123 ” ).
2.3 --- state diagrams
Unlike in math, “ = ” does not mean equals. It is an assignment and is not commutative. Thus, while the variable x = 5, the value of 5 =/= x. The variable name must always be on the left and the value of the variable must always be on the right. Additionally, one variable can be set to equal another until the value of either value is declared to be something else. State diagrams can be used to show the current state of each variable present in the program. Naturally, these values will change as the program is run.
2.4 --- printing variables
By using print or println, the value of a variable can be displayed. This can be done by typing System.out.println(variablename);. If the name of the variable is desired, for whatever reason, that must be typed out in quotes since printing a variable will print its value, not its name. For statements that end in print, there must be a following statement that ends in println for anything to print on most computers.
2.5 --- arithmetic operators
Operators are how mathematical computations are achieved in Java. The character for the addition operator is “ + ”, subtraction operator is “ - ”, multiplication operator is “ * ”, and the division operator is “ / ”. When a program is run, the current value of a variable is used in an expression, followed by the application of any operators. Operands are the values that operators will be working with. Thus, expressions tend to be a combination of numbers, variables, and opeators. These expressions become one value when the program is compiled and executed.
All operations, save for division, work as they normally would. In Java, “ / ” is the character used for what is known as integer division. Since a value that is stored as an integer eliminates any number beyond the decimal point, when one divides 3 by 2, the answer is displayed as 1 instead of 1.5. That said, it may be me accurate to find the percentage of a number that another one is instead of the exact value in some cases. When one has $20 and has spent $19 of that amount and would like to express that fraction to the system, integer division would turn that result into 0. But when that value is multiplied by 100 and then divided by 20, the percentage would be more accurate, resulting in 95% of the money spent.
2.6 --- floating - point numbers
Another way of doing things is by using floating - point numbers. These numbers have the ability to represent fractions in addition to integers. In order for Java to perform floating - point division, one or more of the operands must be floating - point values, such as a double, which can store decimal numbers. The name “ double ” is short for “ double precision ”. Still, using double values may result in confusion. Though 1 and 1.0 are technically the same number, Java will insist on distinguishing the two since their difference is similar to that between a capital and lowercase letter. They are the same, but they are not the same. Integers and doubles are two different data types.
When assigning variables, be sure that int variables receive int values and double variables are given double values. This will prevent error in the first case, and prevent improper form in the other case. If an integer value is assigned to a double and then divided, it will divide as if an integer and then convert to a double once the operation is complete. So if 1 / 3 was declared as the value of a double called y, the resulting value would be 0.0 instead of .333. If one were to instead divide 1.0 by 3.0, the result value would be the correct one.
2.7 --- rounding errors
Since floating - point numbers are only correct in the sense that they are rounded properly, it would be more accurate to use integers. Though those numbers cannot expand beyond the decimal place, shifting them by multiplying by 10 or 100 would yield more accurate results in the end. They can easily be shifted back. These rounding errors occur because not all numbers are properly represented. Repeating decimals are in some cases shown as terminating decimals and the values become skewed.
2.8 --- operators for strings
Strings cannot take part in mathematical operations, regardless of whether or not what is printed is a number by appearance. The only operator that does work with strings is the + operator, but it does not add strings together like numbers --- instead, it appends them. For example, System.out.println(1 + 2 + “Hello”); would print “3Hello”. The numbers outside of quotations would be added and placed before the string. These operations are carried out from left to right.
When more mathematical operations are taking place, Java instead follows the order of operations. I don’t think that that is something I really need to explain for myself or anyone else.
2.9 --- composition
Now that how to combine values and how to display them is known individually, now comes displaying those combined values. An arithmetic expression can be placed within a print statement, for instance. Variables can also be declared and then subsequently assigned to a value by placing an expression on the right side of an assignment.
2.10 --- types of errors
Compile - time errors stem from code that violates basic syntax like forgetting a semicolon or misspelling a word or keyword. A compiler will omit an error and only acknowledge it at the end, leaving the discovery of the error up to the coder. This is referred to as parsing --- reading through a program before translating it. 
Run - time errors are not made apparent until the program as already started running, when the interpreter is executing the byte code and something goes awry. These errors are referred to as exceptions and can be very bad. Error messages will often be given with clues as to where the error took place.
Logic errors occur when a program runs without any issues, but the desired result will not be achieved. This is the easiest error to pinpoint and will just require a closer look at the code to understand what was conveyed incorrectly to the system.
2.11 --- vocabulary
variable: A named storage location for values. All variables have a type, which is declared when the variable is created.
value: A number, string, or other data that can be stored in a variable. Every value belongs to a type ( for example, int or String ).
declaration: A statement that creates a new variable and specifies its type.
type: Mathematically speaking, a set of values. The type of a variable determines which values it can have.
syntax: The structure of a program; the arrangement of the words and symbols it contains. 
keyword: A reserved word used by the compiler to analyze programs. You cannot use keywords ( like public, class, and void ) as variable names. 
assignment: A statement that gives a value to a variable. 
initialize: To assign a variable for the first time.
state: The variables in a program and their current values. 
state diagram: A graphical representation of the state of a program at a point in time. 
operator: A symbol that represents a computation like addition, multiplication, or string concatenation. 
operand: One of the values on which an operator operates. Most operators in Java require two operands. 
expression: A combination of variables, operators, and values that represents a single value. Expressions also have types, as determined by their operators and operands.
floating-point: A data type that represents numbers with an integer part and a fractional part. In Java, the default floating - point type is double. 
rounding error: The difference between the number we want to represent and the nearest floating-point number. 
concatenate: To join two values, often strings, end-to-end. 
order of operations: The rules that determine in what order operations are evaluated. 
composition: The ability to combine simple expressions and statements into compound expressions and statements. 
compile-time error: An error in the source code that makes it impossible to compile. Also called a “ syntax error ". 
parse: To analyze the structure of a program; what the compiler does first. 
run-time error: An error in a program that makes it impossible to run to completion. Also called an “ exception ". 
logic error: An error in a program that makes it do something other than what the programmer intended.
0 notes
minurvaaa · 6 years ago
Text
CHAPTER ONE.
the way of the program
It is important to remember that problem solving is the most important skill for a computer scientist to have, acquire, & develop. Without the ability to break down an issue to its simplest form and working one’s way through to a solution, a computer scientist would fail at their job the majority of the time. Thus, learning how to properly program & learning how to problem solve go hand in hand, serving two ends.
1.1 --- what is programming ?
As defined by the textbook, a program is a sequence of instructions that specifies how to perform a computation. That said, such computations can be either math - based or text - based. For example, a program could be given the job to solve an equation or be crafted in a way that it will search for & / or replace text. Though it seems complex, this is the reality of what a program is.
1.2 --- what is computer science ?
Computer science is a science that is entirely based on the execution of algorithms that, when compiled, make up a program. These algorithms are the steps that are taken in order to alleviate the problem that the program is attempting to solve. Of course, when designing these programs, one is bound to make errors. The action of finding these issues & fixing them is referred to as debugging. This serves as a great workout for the brain & may even help one figure out better ways to write algorithms in the future.
1.3 --- programming languages
The programming language that will be explored in this course is Java, a high - level language. As with all high - level programming languages, Java must be translated into machine language --- a low - level language. This, unfortunately, takes some time. For this inconvenience, however, there are two advantages to using this kind of programming language. Even if the translation process takes time, it takes less time to program in a high - level language, which more than makes up for it. They are also portable and able to be run on any computer they are moved to.
There are two ways of translating a program: an interpreter, a program that reads the high - level code and follows it, or a compiler, a program that reads the high - level code and completely translates it. By way of compiling, the Java source code can be made into object code or an executable. These programs run faster than interpreted ones because they do not need to be compiled to be run again. Byte code serves as the middle ground between the high - level language and the machine language as the Java compiler will instead translate high - level code into byte code before allowing that code to be interpreted ( through the use of the “ Java Virtual Machine ” ) into machine language. So, simply put, Java makes use of a compiler and an interpreter.
This can all be done without nearly as much thought, but it is always nice to understand the process.
1.4 --- the hello world program
This is the first program that most people write when they are first learning how to code --- a displayed message that says “ hello, world ! ”. Though the output is an underwhelming one, the depth of knowledge required to even have the system to even display it has more to it. The program is composed of classes and methods, and methods are, in turn, made up of statements that define them. These statements execute simple operations. The statement that is used here --- System.out.println(“Hello, World!”); --- is referred to as a print statement. Since Java is case - sensitive, this statement must be typed exactly the same each time or it will never run. 
The method that this program is defined as main and contains all statements. Methods, additionally, are held in classes, collections of methods. Classes are usually defined with names starting with a capital letter. The class must always match the name of the file. If the file is named hello.java, the class must be named hello.
To group things in Java, curly braces ( “{” & “}” ) must be used. The methods of the program are contained within the curly braces of the classes they belong to while statements are contained within the curly braces of the methods they define. To add a line of text to the code that does not change it in any sort of way, two slashes ( // ) must be at the beginning of that line.
1.5 --- displaying strings
Any number of statements can be included in main. Meaning, one can display as many messages as they’d like. These messages, referred to as strings, can contain letters, punctuation marks, symbols, spaces, tabs, and others. System.out.println automatically sends code to the next line. If print is used in place of println ( ex: System.out.print ), the following code will not appear on the next line.
1.6 --- escape sequences
Using an special character such as “/n” will allow you to add another line to the string without the creation of another. This is what is referred to as an escape sequence. There should be no space between the escape sequence and the word following or preceding it unless one desires there to be an additional space when the statement is printed / displayed. By using a backslash before a quotation mark ( /” ), one can include a quote within the string without interrupting it.
1.7 --- formatting code
A lot of things can be left to the coder’s own discretion when formatting code, but other things are necessary for the code to run properly.
There must always be spaces between words, but any other sort of spaces are allowed. Newlines are optional as well. Even so, one must take into account the fact that their program’s code must not only be easy for the creator to understand but for anyone else who uses it to be able to understand it as well. Without allowing for others to comprehend the components and purpose of their code, the computer scientist will fail at a key element of their job. This also allows for easily pinpointing / debugging problems in the code.
Google has its own standards that can be found here.
1.8 --- debugging code
It’s a good idea to intentionally make mistakes when learning how to code so that those mistakes can be worked through and the method for fixing them can be understood. Debugging is a necessary part of programming & demands that a computer scientist is aware of how to carry out the process. In that same vein, the process can grow frustrating, so it is okay to ask for help from others that are more experienced.
1.9 --- vocabulary
problem - solving: The process of formulating a problem, finding a solution, and expressing the solution. 
program: A sequence of instructions that specifies how to perform tasks on a computer. 
programming: The application of problem - solving to creating executable computer programs. 
computer science: The scientific and practical approach to computation and its applications. 
algorithm: A procedure or formula for solving a problem, with or without a computer.
bug: An error in a program. 
debugging: The process of finding and removing errors. 
high-level language: A programming language that is designed to be easy for humans to read and write. 
low-level language: A programming language that is designed to be easy for a computer to run. Also called “ machine language ” or “ assembly language ". 
portable: The ability of a program to run on more than one kind of computer. 
interpret: To run a program in a high-level language by translating it one line at a time and immediately executing the corresponding instructions. 
compile: To translate a program in a high-level language into a low-level language, all at once, in preparation for later execution. 
source code: A program in a high-level language, before being compiled. 
object code: The output of the compiler, after translating the program. 
executable: Another name for object code that is ready to run on specific hardware. 
byte code: A special kind of object code used for Java programs. Byte code is similar to a low-level language, but it is portable like a high-level language. 
statement: Part of a program that specifies one step of an algorithm. 
print statement: A statement that causes output to be displayed on the screen. 
method: A named sequence of statements. 
class: For now, a collection of related methods. ( We will see later that there is more to it. ) 
comment: A part of a program that contains information about the program but has no effect when the program runs. 
string: A sequence of characters; the primary data type for text.
newline: A special character signifying the end of a line of text. Also known as line ending, end of line ( EOL ), or line break.
escape sequence: A sequence of code that represents a special character when used inside a string.
0 notes