This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
print("2+5") | |
print(2+5) | |
print(3/5) vs. print(3//5) | |
print(2+3//5) vs. print((2+3)//5) | |
print(10 / 5 - 20 // 6 + (20 + 4) / (3 * 3 - 1)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
float speed; //determines speed of target and missile | |
//centre of missile (line) attributes | |
float missileX = -200; | |
float missileY = -200; | |
float missileLength = 0; | |
//tank attributes | |
float tankY = 0; | |
float tankHeight = 0; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int x = 2; //range: -2 to the 31 to 2 to the 31 minus 1 | |
int n = 30; | |
//x to the power of n is defined as x * x * x * ... n times | |
int result = 1; //answer we are going to "build" (multiplicative identity is 1) | |
int i = 1; //loop variable to count how many times loop should execute | |
while(i <= n) { | |
result = result * x; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// String Introduction | |
String unitCode = "COMP1000"; | |
//internally, there is a char[] named elements that is created to hold the 8 characters | |
String str; // declare | |
str = "Peach"; // initalise | |
String myString = "Hello World"; // declare and initialise on the same line. | |
println("number of characters in "+unitCode+": "+unitCode.length()); | |
println("number of characters in "+str+": "+str.length()); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int[] ali = {10, 70, 20, 90}; | |
int[] bob = {30, 10, 40}; | |
int[] cindy = {100}; | |
int[] dina = null; | |
int[][] marks = {ali, bob, cindy, dina}; | |
/* | |
reference copies :) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//check if string is purely numeric | |
boolean isNumeric(String s) { | |
if(s==null || s.length() == 0) { | |
return false; | |
} | |
//guaranteed: s has at least one character | |
for(int i=0; i < s.length(); i++) { | |
char c = s.charAt(i); | |
if(c < '0' || c > '9') { | |
return false; //at least one character ain't a digit |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
String a = "Taxi"; | |
String b = "Taxi"; | |
println("a == b: "+ (a == b)); | |
println("a.equals(b): "+ (a.equals(b))); | |
String c = "Cab"; | |
String d = new String("Cab"); //force a new instance | |
println("c == d: "+ (c == d)); | |
println("c.equals(d): "+ (c.equals(d))); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
String name = "Messi"; | |
println("number of characters in \""+name+"\": "+name.length()); | |
println("first character: '"+name.charAt(0)+"'"); | |
println("last character: '"+name.charAt(name.length()-1)+"'"); | |
for(int i=0; i < name.length(); i++) { | |
print("'"+name.charAt(i)+"'"); | |
if(i < name.length()-1) { | |
print(", "); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
String removeAllOccurrences(char ch, String s) { //BUGGY :( | |
for (int idx=0; idx < s.length(); idx++) { | |
if (s.charAt(idx) == ch) { | |
String before = s.substring(0, idx); | |
String after = s.substring(idx+1); | |
s = before + after; | |
idx--; //check the character after again | |
} | |
} | |
return s; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package comp6010_week_5_lecture; | |
import java.util.Arrays; //you are NOT allowed to have this in assignment 2 | |
public class ArrayClient { | |
public static void main(String[] args) { | |
int x = -13; //primitive data typed variable | |
int y = 500; | |
System.out.println("x="+x); |
NewerOlder