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