



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
All syntax and programming paradigm in java
Typology: Cheat Sheet
1 / 7
This page cannot be seen from the preview
Don't miss anything!
By now, you should know this by heart! Eclipse has a kind soul and remembers the first line for you when you start a new class, but the rest is all on you. public class HelloWorld { public static void main(String[] args) { System.out.println(“Hello World!”); } }
Comments are a useful way to document your program. They are ignored by the compiler. You can type in anything you want like a description of the segment of code you are writing. This is also useful for other programmers that may be using or reading your code. A comment starts with two forward slashes //. Anything that comes after the // up to the end of the line is a comment. Another form of the comment is the /* / pairing, where everything inside the start / and end */ becomes a comment. public class CommentsTest { public static void main(String[] args) { System.out.println(“This program is a demonstration of comments.”); // Comments are lines that are not looked at by the compiler (Eclipse) // This is a comment that has // been put on more than // one line /* This is a comment that has been put on more than one line */ // System.out.println(“Comments work great for ignoring lines of“); // System.out.println(“code that you’re not sure you need…”); }
Just as in algebra, variables store values. Variable names can be any combination of letters, numbers and the underscore () so long as it starts with a letter or an underscore. Associated with all variables is a data type (int, double, bool, char, String). A variable’s data type can never change once it has been declared._ public class SimpleCalculator { public static void main(String[] args) { int x = 5; int y = 7; int z = x + y; // ints (short for integers) are numbers without // decimals double a = 9.4; double b = 2.333333333; double c = a / b; // doubles are numbers with decimals bool winning = true; // bools (short for Boolean) are variables // that can only be true or false char myFavoriteLetter = ‘j’; // chars are surrounded by ‘single quotes’ String myName = “Scrabble”; // Strings (capital S) are groups of // chars. They are surrounded // by “double quotes”. } }
In Java, we need to import a built-‐‑in class called Scanner. We do this on the very first line of your program. Then, in your “main” method, we create a new Scanner, and use it (it’s called “s” in this example) to get a nextInt() or a nextLine(), etc. and store the user input into a variable with an appropriate data type. import java.util.Scanner; public class AskName { public static void main(String[] args) { Scanner s = new Scanner(System.in); System.out.println(“What is your name?”); String name = s.nextLine(); System.out.println(“Nice to meet you, “ + name);
System.out.println(“Enjoy retirement!”); } else if(age > 50) { System.out.println(“You must still enjoy your job…”); } else if(age > 30) { System.out.println(“How are the children?”); } else if(age > 18) { System.out.println(“How’s college?”); } else if(age > 10) { System.out.println(“Enjoy childhood while you can.”); } else if(age < 10 && age > 0) { System.out.println(“You’re too young for a computer…”); } else { System.out.println(“Liar…”); } } }
A condition can be a comparison or any kind of Boolean value. Before, looping the code in the body of the while, the program checks to see if the condition inside the parentheses is true. If it is, the body will start executing, and keep executing until the condition becomes false. public class MyGame { public static void main(String[] args) { int numberOfLives = 5; while(numberOfLives > 0) { // makes sense, right? // put your game code here…. if(dead == true) { numberOfLives-‐‑-‐‑; // a quick way to subtract one // from numberOfLives. } } } }
A for loop is a similar to a while loop, but still depends on a Boolean condition looping. You can initialize a value for counting, then compare it against another value and change the value (known as incrementing). public class ForTest { public static void main(String[] args) { for(int i = 0; i < 4; i++) { System.out.println(“The number is “ + i);
Notice that this for loop does the same thing as the while loop example above. This is a list of steps that a for loop goes through:
_1. initialize the counter variable (i)
Just like we imported the Scanner class, now we need to import the Random class to generate random numbers. Once we import the Random class and create a Random object called “r”, we can have “r” give us a nextInt(), nextDouble(), etc., and manipulate those numbers till we get the range of our choosing. import java.util.Random; public class RandomTest { public static void main(String[] args) { Random r = new Random(); // print 10 random integers for(int i = 0; i < 10; i++) { System.out.println(r.nextInt()); } // print 10 random integers between 0 and 5 for(int i = 0; i < 10; i++) { System.out.println(r.nextInt(5)); } // print 10 random integers between 20 and 30 for(int i = 0; i < 10; i++) { System.out.println((20+r.nextInt(10))); } } }