Filed under: Uncategorized
On this blog I will post tutorials that I”ve written and links to ones that I”ve found that I enjoy. Notes from my Software Development courses will be posted here as well as long with other things I find useful for the maths, sciences, arts, and technology. I do private tutoring in everything from Photoshop, or 3d, to biology, Microbio, and anatomy for people in the Pittsburgh area.
Filed under: Class Notes, Java | Tags: CCAC, CIT111, Class Notes, Java, java arrays, loops, methods, programming
ARRAYS ARE:
–Primitive variables are designs to hold only one value at a time
–allows to create collection of like indexed values
–can store any type of data but only one type at a time
–List of data elements
Creating Arrays
–An array is an object so it needs to be referenced. Then create array and assign address
int[]nameofarray=new int [INTEGER VALUE]
Integer value can be a variable, a constant etc but always must be INT.
–Whatever data type is stored in the array determines how it is initialized. If it’s created as an INT all values start at 0.
–may be of any type (floats, car, long, double etc)
ARRAY SIZE: must be a non negative number. It may be a literal, a constant, or avariable
ex: final int ARRAY_SIZE=6
int[] numbers=new int[ARRAY_SIZE]
–Once created the size is fixed and cannot change!!
USING INDEX VALUES
–The first element is ALWAYS INDEX 0. The last element in the array is always the size or length -1
In an array with 6 indexes. The first one is index0, the last one is index5
Calling an index:
arrayname[indexnumber]= new value //assigning variables to be held in the index
arrayname[0]=17
An array is accessed by:
–The reference name
–the subscript [] which declares what to access
Can be treated like any other variable of the same type.
(more…)
Filed under: Uncategorized | Tags: CCAC, CIT111, Class Notes, Java, loops, methods, newbie java
Here’s an example of a method that returns the smallest number out of a set of three entered by the user:
(more…)
Filed under: Uncategorized | Tags: CIT111, Class Notes, Java, methods, tips
Methods:Stand alone units, or blocks of code that do a specific task.
–EXAMPLE: public static void main(String []args) <—a main method
–the "public" means it's public, "void" returns nothing, "(String[]args)" returns the string
–
Step-line Refinement: Creating routines and methods to break the code down into more manageable and efficent parts.
MAKING A METHOD
1) Call the method inside the main method: any method that is called inside the main must also be STATIC!
Example: public static void printStar()
{
}
KEY POINT:METHODS CAN NEVER BE INSIDE ANOTHER METHOD! It’s inside the class, but outside any other method!
2) next put the required method code inside the {} to make the method, making sure that the block of code stands alone.
3)INVOKING THE METHOD: put the method in the code by putting the method name and ()
Programmer Ref Cards from Dzone. Free Sparknotes style cheat sheet references for programmers and students. Registration to the site is free too!
Filed under: Finches, Java | Tags: CIT111, Class Notes, CMU, Finch, Java, loops
Here’s some code to make the CMU Finch count down from 10 and sing.
/**
* Created by:
* Date:
* make baby Finch count and change nose color to a random tint with each number
*/import finch.*;
import javax.swing.JOptionPane;
import java.util.Random;public class finchCount
{public static void main(final String[] args)
{
// Instantiating the Finch object
Finch baby = new Finch();
Random spin = new Random();
int count, redNum, greenNum, blueNum;
String input; //name for what you’re asking for
input = JOptionPane.showInputDialog(null, “What number do you want to count to?”);
count = Integer.parseInt(input);
for(int i = 1; i <= count; i++)//variable I; while I is lessthan/equal to COUNT, keep adding to I
//never a ; here
{
baby.saySomething(“”+ i);
baby.sleep(1000);
//turn nose to random color
redNum= spin.nextInt(256);//random num between 0 and 255
greenNum= spin.nextInt(256);//random num between 0 and 255
blueNum= spin.nextInt(256);//random num between 0 and 255
baby.setLED(redNum,greenNum,blueNum);
baby.sleep(1000);
System.out.println(“number ” +i);
}
// Always end your program with finch.quit()
baby.quit();
System.exit(0);
}
}How about a fun nose color change too? (more…)
Filed under: Uncategorized | Tags: CIT111, Class Notes, infinite loop, Java, loops
While loops mean “while this statement is true, do this. When it’s false do something else. A loop is a part of a program that repeats until a certain need is met
Every “WHILE” loop that you write has certain parts. If you forget these, you can end up with an infinite loop and you don’t want this. Infinite loop goes on forever (the dreaded hourglass in windows, the spinning pinwheel of death in mac…) and THIS IS BAD! Don’t be like Microsoft, loop well.
With all loops:
Initialize
Test
Update
Make sure not to close your loop prematurely with a “;”
Ex:
while (number MAX_INT) // NO “;” IN A WHILE LOOP; && cannot work for this, instead use OR which is || {
JOptionPane.showMessageDialog(null,”You must not be able to read. Try again, Ceiling Cat is watching! “);
inData = JOptionPane.showInputDialog(” Please enter a number from 1 to 10″);
number =Integer.parseInt(inData);//make string a number datatype
}
Lets look at this little program:
/**
This program demonstrates the while loop.
*/public class WhileLoop
//while is a KEY WORD and has special meaning. “While” is followed by a boolean expression inside a {}
//while “this bit of bode is doing its thing and this condition is true, then do this command. when it’s FALSE stop doing.
{
public static void main(String [] args)
{
int number = 1; //this INITIALIZESwhile (number <= 5) //this TESTS
{
System.out.println("Hello");
number++;//this UPDATES the loop
}System.out.println("That's all!");
}
}
Keep in mind that {}s are your friends. They link bits of code together like putting beads in different boxes in a kit. Lets do a tracethrough of this little program: (more…)
Filed under: Uncategorized
Even the most basic programs have to make choices. We do this with “if-then”, “if-else”, “if-else-if”, “Switch” and other decision structures.
Switch Control Structure: acts like an if else statment; lets user pick an option and ‘switches’
Start with SWITCH which is a keyword and tells the program to evaluate that variable (in example is “number”). Switch can only compare INT or CHAR variables. BREAK after each case pops out of the control structure.
import java.util.Scanner; // Needed for Scanner class
/**
This program demonstrates the switch statement.
*/public class SwitchDemo
{
public static void main(String[] args)
{
int number; // A number entered by the user// Create a Scanner object for keyboard input.
Scanner keyboard = new Scanner(System.in);// Get one of the numbers 1, 2, or 3 from the user.
System.out.print(“Enter 1, 2, or 3: “);
number = keyboard.nextInt();// Determine the number entered.
switch (number)
{
case 1:
System.out.println(“You entered 1.”);
break;
case 2:
System.out.println(“You entered 2.”);
break;
case 3:
System.out.println(“You entered 3.”);
break;
default:
System.out.println(“That’s not 1, 2, or 3!”);
}
}
}
Switch control structures are easier to read than other forms
Filed under: Uncategorized | Tags: CIT111, Class Notes, incrementing, Java, newbie java
With incrementing and decrementing, things can be in many different orders. You can prefix, or postfix and write it in different ways.
Example: “++number” is the same as “number++” or “number +=1″.
What happens with prefixing:
Say you have a number 4 and ++number. When it runs the program it will starting 5, not 4. If you put it after, then it will start with 4. Here’s a small example program.
/**
This program demonstrates the ++ and — operators.
*/public class IncrementDecrement
{
public static void main(String[] args)
{
int number = 4; // number starts out with 4// Display the value in number.
System.out.println(“number is ” + number);
System.out.println(“I will increment number.”);// Increment number.
number++; //number=number + 1; number += 1 Can put ++ or — infront of variable// Display the value in number again.
System.out.println(“Now, number is ” + number);
System.out.println(“I will decrement number.”);// Decrement number.
number–; //number=number-1; number -=1// Display the value in number once more.
System.out.println(“Now, number is ” + number);
}
}
The result of this program shows up as:
number is 4
I will increment number.
Now, number is 5
I will decrement number.
Now, number is 4
Process completed.
If PREfixed, the program spits out
number is 5
I will increment number.
Now, number is 6
I will decrement number.
Now, number is 5
Process completed.
Filed under: Uncategorized
String classes and Methods
Each string class has methods to manipulate them. Any input from GUI windows come in as a string datatype and must be changed into the correct classes. String classes are constant and their value can’t be changed after they are created.
A string class is a complex data type, sort of like an ‘array’ but it’s more than just that. For example, in strings, the characters are indexed by number. Index = “length -1″
A list of every string method can be found under strings at Java 6 API Docs. This is handy if you don’t know what the method is or just want to know more.
Booleans: Can hold true or false, can be assigned any way that we want them, as long as the expression evaluates to “true or false”.
Example:
// A program for demonstrating boolean variables
public class TrueFalse
{
public static void main(String[] args)
{
boolean bool;
int a=18, b=12;
bool = true;
System.out.println(bool);
bool = false;
System.out.println(bool);
bool = a > b;
if (bool)
{System.out.println(“‘a’ is larger than ‘b’”);
}
}
}
Should come out to say:
true
false
‘a’ is larger than ‘b’Process completed.
CHARS: are letter based only; can only hold a single character.
// This program demonstrates the char data type.
public class Letters
{
public static void main(String[] args)
{
char letter;letter = ‘A’;
System.out.println(letter);
letter = ‘B’;
System.out.println(letter);
}
}
COMMON MISTAKES WITH CHARS:
-Errors occur when more than one character is used. Char can only use ONE.
-Double quotes “” around the single character makes it the wrong data type. Anything with “” is a STRING not a CHAR!! Single quotes must always be used
Chars and Integer relationships: Letters can be assigned using numeric value for a letter in Java 6.