Commit 31e68cf5 authored by michael lundquist's avatar michael lundquist
Browse files

Adding notes from ch1 and starting on code for ch1

parent d24fd138
import java.util.Scanner;
//import
public class Main{
public static void main(String[] args){
java.util.zip.Deflater def;
}
/**
* Checks if a nubmer is even. Exercise 1.4
*
* @param i the input to test for evenness
* @return true if it's even, else false.
*/
public static boolean isEven(int i){
if((i & 1) == 1)//deletes all the other digits, leaving only the 1s place
return false;
return true;
}
/**
* R-1.5
* returns the sum of all positive integers up to and including n
* @param n
* @return
*/
public static int sumToN(int n){
return (n + 1 * (n/2)) + (isEven(n)? 0: ((int) n/2) + 1);
}
/**
* R-1.6
* gives the sum of all the odd positive integers up to and including n
* @param n
* @return
*/
public static int sumOddsToN(int n){
int val = 0;
if(!isEven(n)){
val = ((int) n/2) + 1;
}
int preSum = sumToN(n);
return preSum/2 +n;
}
}
\ No newline at end of file
......@@ -3,7 +3,7 @@
## 1.1 Getting Started
- The book describes writing hello world, java key words...
- __method__: executible statements inside java functions
- __method__: executable statements inside java functions
- __class__
- __block__: code contained with braces ("{" and"}")
......@@ -42,7 +42,7 @@
- _double_: 64-bit floating-point number (IEEE 754-1985)
- 8 bytes, 16 decimal digits `double e = 2.72342;`
- In java, uninitalized base types variables are assigned a default value. By default:
- In java, uninitialized base types variables are assigned a default value. By default:
- numeric types are `0`
- boolean types are `false`
- character types are `null`
......@@ -100,7 +100,7 @@
- __Access control modifiers__: control the level of access (aka *visibility*) the defining class grants to other classes.
- __public__: all classes may access this member using dot notation
- in java, public (non inner) classes must be defined in their own file whose file name matches the class name.
- in java, public (non inner) classes must be defined in their own file whose file name matches the class name. You may only have 1 public, non-inner class per file.
- __Protected__: access is only granted to *subclasses* of the class and other classes in the same *package*
- __subclasses__: classes that inherit from a class (see 2.2)
- __package__ (see 1.8)
......@@ -122,7 +122,7 @@
##### The __final__ Modifier
- variables initialized with the final varaible can't be reassigned.
- variables initialized with the final variable can't be reassigned.
- final reference variables always refer to the same object
- final variables are typically static
......@@ -149,7 +149,7 @@
- see earlier for public, private and static modifiers.
- the *returnType* defines what type of value the function returns, or `void` for no return value.
- each paramater has a *type* and a name (*paramName* in this example)
- each parameter has a *type* and a name (*paramName* in this example)
- the parameter list can be empty
##### Return types
......@@ -184,7 +184,7 @@
- Constructors can't be __static__, __abstract__, or __final__ only the access modifiers are allowed (private, public, protected)
- constructors don't have a return type, and don't return anything. The __new__ operator that's called with the constructor gets a reference to the created object.
- like methods, constructors can be overloaded with different signitures.
- like methods, constructors can be overloaded with different signatures.
- Java provides a default constructor with no parameters. If you create a custom overloaded constructor, the default constructor isn't provided.
#### The Keyword __this__
......@@ -197,7 +197,7 @@
#### The main Method
- Java classes that have a __main__ method are executible from the command line.
- Java classes that have a __main__ method are executable from the command line.
- main method syntax:
......@@ -219,7 +219,7 @@ public static void main(String[] args){
## 1.3 Strings, Wrappers, Arrays and Enum Types
### The String Class
### The String Class
- Java's __char__ base type stores a single character from the 16-bit unicode international alphabet.
- character literals are surrounded by single quotes, 's'.
......@@ -269,7 +269,7 @@ public static void main(String[] args){
### Arrays
- Arrays store homoginous data. Each cell (element) in an array is stored at an index.
- Arrays store homogeneous data. Each cell (element) in an array is stored at an index.
#### Array Elements and Capacities
......@@ -326,12 +326,12 @@ public static void main(String[] args){
#### Arithmetic Operators
- The arithmetic operators are:
- + addition
- - subtraction
- * multiplication
- \+ addition
- \- subtraction
- \* multiplication
- / division
- % modulo
- - uniary negation
- \- unary negation
- parentheses can be used to define the order of operations.
- Java doesn't allow operator overloading for class types.
......@@ -538,7 +538,7 @@ public static double sum(double[] data){
### Simple Output Methods
- Java's print methods print to "standard output". Generally this goes to the shell, but can be redirected, for example, to a file.
- Java's `System.out` is an instance of `java.io.PrinteStream` which stores output in a *buffer* before it prints it empties it to the console.
- Java's `System.out` is an instance of `java.io.PrintStream` which stores output in a *buffer* before it prints it empties it to the console.
- System.out print methods:
- `print(String s)` Prints s
......@@ -593,4 +593,165 @@ public class InputExample{
## 1.8 Packages and Imports
-
\ No newline at end of file
- Java allows related type definitions to be grouped into a __package__.
- remember, type definitions are:
- __classes__, which have some [rules about access modifiers at the file level](#Access-Control-Modifiers) as you'll recall. These rules make packaging unambiguous.
- __enums__, which have this [syntax](#Enum-Types) as you'll recall
- For types to belong to a package named *packageName*, they must be located in a directory named *packageName* and each file must begin with the line:
<pre>
<b>package</b> <i>packageName</i>;
</pre>
- Package names are lowercase by default
- Files that don't explicitly declare a package are placed in the *default package*
- You can use a class in a package without importing it by using its fully qualified name (using dot notation).
- For example, `java.util.zip.Deflater def;` is a valid initialization even without importing java.util.zip.*
- *subpackages* are located withing a subdirector of a package's directory and their fully qualified name starts with the superpackage's fully qualified name.
- for example, `java.util.zip` is a subpackage of `java.util`
- advantages of using packages:
- packages prevent name space cluttering.
- two classes can have the same name as long as their fully qualified name is different.
- distributing packages is easy
- self contained packages are easy to understand.
- packaging defines the functionality of the __protected__ access modifier.
### Import Statements
- the __import__ keyword allows you to include external classes or entire packages into the current file.
- to import a specific class from a package put __import__ *packageName.ClassName*; at the top of the file, then you can use the class as if it were in your current package.
- Note, it is illegal to import classes that are already defined in the current file, or that conflict with another imported class.
### Importing a Whole Package
- You can include all the types from a package with __import__ *packageName*.*
- When importing a whole package:
- if a type from the package conflicts with a local type, the local type is preferred and the imported type must be qualified.
- if types from two imported packages conflict, neither can be used without qualification.
## 1.9 Software Development
- Software dev has 3 steps:
1. Design
2. Code
3. Test and Debug
- This section covers these steps along with coding style, naming conventions, formal documentation and testing.
### 1.9.1 Design
- Design involves dividing the program into classes.
- Each class should have *responsibilities* and *behaviors* that are *independent* from other classes.
- Write a *protocol* for how other classes should interact with your class
- Use __Class-Responsibility-Collaborator (CRC) cards__ to define classes. On each CRC card define one class by:
- Writing the name of the component at the top
- Writing the responsibilities on the left
- writing collaborator classes required for the class to work on the right
- When done, you should have a CRC card for each actor in your program.
- CRC cards should be written on index cards to force you to use small classes.
- Unified Modeling Language (UML) is often used to express the organization of a program.
- One UML tool is the *class diagram* which has 3 portions:
- the name of the class at the top
- the class's fields (instance variables) in the middle
- The class's methods at the bottom
- class diagrams also specify access modifiers:
- \- for __private__
- \# for __protected__
- \+ for __public__
### 1.9.2 Pseudocode
- Before writing code, programmers often write __pseudocode__ to explain the algorithm in a simple, code like, terms that they can folllow as they implement their code.
- Pseudocode should include:
- __Expressions__ (like ==)
- __Method declarations__
- __Decision structures__
- __while-loops__: *do*...*while*
- __repeat-loops__: *repeat*...*until* (similar to a while)
- __For-loops__: *for*...*do*
- __Array indexing__
- __Method calls__
- __Method returns__
- __Comments__
### 1.9.3 Coding
- *design patterns* provide a template for classes, their data and methods, and how they interact with each other.
- *IDEs* are a thing
- you have to compile .java files into .class files (or a .jar package)
- the commandline command for this is `javac`, or use an IDE's run buttons
- compiling can identify syntax errors.
- use the `java` commandline command to run a .class file, or use an IDE's run buttons
- When running `java`, java searches the __CLASSPATH__ envoronment variable to find the class's name you passed as an argument. Generally starting the search with the current directory.
### 1.9.4 Documentation and Style
#### Javadoc
- Java comes with a documentation producer called *javadoc* that renders *tags* from block comments that are surrounded with `/**` and `*/` into HTML.
- javadoc comments must come just before a class definition, instance variable declaration, or method definition
- Java doc comments start with a description, then tags. Some of these tags are:
- @author *text*
- @throws *exceptionName description*
- @param *parameterName description*
- @return *description
#### Readability and Programming Conventions
- Programming style tips:
- use meaningful identifiers that reflect the action, responsibility, or data the identifier is naming.
- Typically, identifiers first letter capitalized, except for in variables and method names.
- Use constants or enums instead of literals. This enhances readability, robustness, and modifiability.
- constants should be all caps, including elements in enums
- indent 4 spaces
- Organize a class in this order:
1. Constants
2. Instance Variables
3. Constructors
4. Methods
- Use comments.
### 1.9.5 Testing and Debugging
- Testing and debugging are the most time-consuming.
#### Testing
- you should have a test plan
- __method coverage__: ensuring every method is run at least once during your test.
- __statement coverage__: Ensuring every statement is run at least once during your test.
- Remember to test __special cases__ where the program gets unexpected data. Special cases can be at the method or statement level.
- Randomly generated test cases often using `java.util.Random` is also good.
- There are 2 general testing philosophies:
- __top-down__: higher up methods (methods that call other methods) are tested first. Typically, lower methods are replaced with a __stub__, or a method that returns a known correct value.
- Stubbing is used in bootstrapping.
- __bottom-up__: starts at lower level classes and methods before testing higher levels.
- aka *unit testing* as the functionality of a specific component is tested in isolation of the larger software project.
- We [discussed how the main method can be used for testing](#Unit-Testing), but testing with the main method is crude. These days, we use frameworks like [junit](www.junit.org) to test. Junit sopports grouping individual tests into larger test suites, and provides support for executing those suites, and reporting or analyzing the results of those tests.
- As software is maintained, *regression testing* should be performed to ensure new changes don't break previously tested components.
#### Debugging
- __print statements__ are the simplest debugging technique.
- using a __debugger__ to insert __breakpoints__ is another approach.
- advanced debuggers allow __conditional breakpoints__ which are executed only if a condition is satisfied.
- java comes standard with the jdb debugger, which has a command line interface, but an IDE is often used.
[1.10 Exercises](./code/Main.java)
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment