Unit 1 Lesson Homework
- Score: 0.9/1.0
- Why to use Java? (1.1)
- Basic Java (1.1)
- Hack 1.1
- List of Data Types (1.2)
- Hack 1.2
- Operators (1.3)
- Hack 1.3
- Assignment operators (1.4)
- Hack 1.4
- Casting and Ranges (1.5)
- Hack 1.5
- Code Example!
Basic Java (1.1)
- All code that runs must be in the main method (shown below)
- To print something, use System.out.print(); and inside the parenthesis put text in quotes (String)
- To print with a new line, use System.out.println();
- To comment your code, use // for single line comments and /**/ for multiple lines (example will be shown below)
- It is important to describe how and why your code works, however dont describe obvious things
public class Example {
public static void main(String[] args) {
System.out.print("Hello World");
System.out.print(" From Team Oops!");
System.out.println(); //done to separate two different lines
System.out.println("Welcome to our presentation!");
System.out.print("We hope you learn something from it!");
}
}
Example.main(null);
public class Printing {
public static void main(String[] args) {
System.out.println("Jazair Tallman");
System.out.println("JEHB");
}
}
Printing.main(null);
List of Data Types (1.2)
- Data types are different categories in which one can store various types of data.
- The main Primitve data types are:
- Integer (int): used for whole numbers
- Double (double): used for numbers with decimals
- Boolean (boolean): used for true or false conditionals
- For Primitive types, variables store actual data instead of reference
- If the variable is declared final, it cannot be edited
- A non Primitive type which is commonly used is String
- Stores text
public class Example {
public static void main(String[] args) {
int Herbo = 10;
double gasPrices = 7.99;
final boolean Hot = true;
String name = "Team Oops is hot:";
System.out.println(Herbo);
System.out.println(gasPrices);
System.out.println(name + Hot);
// Hot = false; cannot assign a value to final variable Hot
}
}
Example.main(null);
public class Biodata {
public static void main(String[] args) {
String name = "Jazair Tallman";
int age = 17;
boolean underclassmen = false;
double height = 5.75;
System.out.println("My name is " + name + " and I am " + age + " years old.");
System.out.println(underclassmen);
System.out.println(height);
}
}
Biodata.main(null);
Operators (1.3)
- In order to perform mathmatical calculations on integers and doubles, you can use operators
- Main ones are +, -, *, /
- These are what you expect
- When dividing integers, it always rounds down because output must be an integer
- When dividing by 0, will get the ArithemticException Error
- Modulus is %, used to get remainder when two numbers are divided
public class Math {
public static void main(String[] args) {
int number = 2;
int number2 = 5;
double number3 = 2.0;
double number4 = 5.0;
System.out.println(number+number2);
System.out.println(number3+number4);
System.out.println(number-number2);
System.out.println(number3-number4);
System.out.println(number * number2);
System.out.println(number3 * number4);
System.out.println(number/number2);
System.out.println(number3/number4);
System.out.println(number4 % number3);
System.out.println(number2 % number);
}
}
Math.main(null);
public class Num {
public static void main(String[] args) {
int num1 = 6 * 1234124;
int num2 = num1 % 11345;
int num3 = num2 - 890809 + 90800;
int num = num3 / 980098;
System.out.println(num / 100);
}
}
Num.main(null);
Assignment operators (1.4)
- += adds value of a variabe to another variable and assigns total value to first variable
- -= subtracts value of a variabe to another variable and assigns total value to first variable
- *= multiplies value of a variabe to another variable and assigns total value to first variable
- /= multiplies value of a variabe to another variable and assigns total value to first variable
- %= takes the remainder of a variable with a second variable and assigns remainder to first variable
- ++ increments a variable by 1, to incrememt by more change second plus to number which you want to incrememnt by
- -- subracts a variable by 1, to incrememt by more change second plus to number which you want to subtract by
public class Operators {
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
System.out.println(i);
}
}
}
Operators.main(null);
public class Cast {
public static void main(String[] args) {
double num = 10.5;
int num2 = 100;
int numInt = (int)num;
double num2Double = (double)num2;
System.out.println(num);
System.out.println(num2);
System.out.println(numInt);
System.out.println(num2Double);
System.out.println(Integer.MAX_VALUE);
System.out.println(Integer.MIN_VALUE);
}
}
Cast.main(null);
public class CastActivity {
public static void main(String[] args) {
double a = 123456.123456;
int b = (int)a;
System.out.println(a);
System.out.println(b);
}
}
CastActivity.main(null);
public class Main {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("What is your name?");
String name = sc.next(); //string
System.out.println(name);
System.out.println("How many pizzas do you want to buy?");
int pizzas = sc.nextInt(); //integer
System.out.println(pizzas);
System.out.println("Do you have the discount (true/false)?");
boolean hasDiscount = sc.nextBoolean(); //boolean
System.out.println(hasDiscount);
double price; //double, defaults to 0
if (hasDiscount) {
price = 1.20;
} else {
price = 2.10;
}
char firstChar = name.charAt(0); //character
double finalPrice = price * pizzas * 1.08; // adding taxes
System.out.println("Hi " + firstChar + "! You have to pay " + (finalPrice) + " dollars.");
}
}
Main.main(null);