College Board (Units 1-2) Blog
Jupyter Notebook on college board units 1-2 learning
Notes
- Primitative Types include
- Boolean (true/false), one bit
- Int (integer values), 2-3 bits
- Double (Decimal values) 64 bits
- Short, Byte, Floats, Char
- Operators include
- /+ is addition
- /- is subtraction
- / is division
- is modulus (remainder)
- /* is multiplication
- ++ ex ++x is x=x+1
- -- ex --x is x= x-1
- += ex x+=3 is x=x+3
- -=ex x-=3 is x=x-3
- Compound assignment operators (+=. -=) can be used in place of regular assignment operators
- ++ and -- are increase and decrease operators
public class CompOpsDemo {
public static void main(String[] args) {
int x = 6;
x += 7;
x -= 7;
x *= 3;
x /= 5;
x %= 3;
}
}
import java.util.Scanner;
public class testScoreApp {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
{
// display operational messages
System.out.println("Please enter test scores that range from 0 to 100.");
System.out.println("To end the program enter 999.");
System.out.println(); // print a blank line
// initialize variables and create a Scanner object
Scanner sc = new Scanner(System.in);
String choice = "y";
// get a series of test scores from the user
while (!choice.equalsIgnoreCase("n"))
{
int scoreTotal = 0;
int scoreCount = 0;
int testScore = 0;
System.out.println("Enter the number of test score to be entered: ");
int numberOfTestScores = sc.nextInt();
for (int i = 1; i <= numberOfTestScores; i++)
{
// get the input from the user
System.out.print("Enter score " + i + ": ");
testScore = sc.nextInt();
// accumulate score count and score total
if (testScore <= 100)
{
scoreCount = scoreCount + 1;
scoreTotal = scoreTotal + testScore;
}
else if (testScore > 100)
System.out.println("Invalid entry, not counted");
}
double averageScore = scoreTotal / scoreCount;
String message = "\n" +
"Score count: " + scoreCount + "\n"
+ "Score total: " + scoreTotal + "\n"
+ "Average score: " + averageScore + "\n";
System.out.println(message);
System.out.println();
System.out.println("Enter more test scores? (y/n)");
choice= sc.next();
}
// display the score count, score total, and average score
}
}
}
Notes
- Object-oriented programming, refered to as OOP, is a programming paradigm that organizes software design around objects allowing data to operate and be bound together
- Classes are a template from which objects are created. Objects created under the same class will share common methods and attributes.
- Objects are instances of a class.
- Methods, or functions, are a set of code that perform a specific task.
- Ex: class vegetables would have objects carrot, tomato, etc.
- Class attributes (which the objects will inherit) could be calorie count, quantity, or other values relevant to the class
- Methods of the class would be actions relevant to the class like consume or save
- Classes in Java can contain data members, methods, constructors, nested classes, and interfaces.
- To initialize an object, you would use a class constructor
- Defining and calling methods is advantageous because it allows for code reuse, optimization, and organization.
- Calling a method: methodName(parameter1, parameter2);
- Calling an object's method: objectReference.methodName(parameter1, parameter2);
Painter myPainter = new Painter(); //Object initialized by calling a constructor
public int max(int x, int y) // modifier, return-type, method=name, parameter-list
{
if (x>y)
return x;
else
return y;
}
methodName(parameter1, parameter2); //Calling a method
objectReference.methodName(parameter1, parameter2); //Calling an object's method
public class Science { // class created
public void printTypes(){ // method
System.out.println("Physics");
System.out.println("Astrology");
System.out.println("Chemistry");
System.out.println("Biology");
}
public static void main(String[] args){ // main class that runs
Science myObject = new Science(); // creating an object from class
myObject.printTypes();
}
}
Science.main(null);
public class ScienceTypes extends Science{ // extending class
public ScienceTypes(){ // new class now has all the old class attributes
}
public static void main(String[] args){ // main class
ScienceTypes mySecondObject = new ScienceTypes(); // creates an object from the new class
mySecondObject.printTypes(); // reference methods and attributes from the inherited class
}
}
ScienceTypes.main(null);