Skip to content

Instantly share code, notes, and snippets.

@MbuguaCaleb
Last active February 17, 2021 13:28
Show Gist options
  • Save MbuguaCaleb/af4f02f015cb81abaf49432c591041ff to your computer and use it in GitHub Desktop.
Save MbuguaCaleb/af4f02f015cb81abaf49432c591041ff to your computer and use it in GitHub Desktop.
Java Fundamentals
//Strings
package com.caleb;
public class Main {
public static void main(String[] args) {
String hello =" CALEB" + "MBUGUA!! ";
//Concat strings
//String is a class therefore it has methods we canbe able to access.
System.out.println(hello.toLowerCase());
}
}
//Escape sequences
package com.caleb;
public class Main {
public static void main(String[] args) {
String message = "Caleb \"Mbugua\"";
String url ="c:\\Windows\\...";
String name ="Mercy \n wanjiru";
String backslash = "c:\t Windows\\...";
System.out.println(backslash);
}
}
//Arrays
package com.caleb;
import java.lang.reflect.Array;
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int numbers[] = new int[5];
numbers[0]=0;
numbers[1]=1;
//converting an array to string otherwise it returns the memory address
// Arrays.toString(numbers);
//Method2
int numbersTwo[] ={2,3,4,1,5};
//sort Array
Arrays.sort(numbersTwo);
System.out.println(Arrays.toString(numbers));
System.out.println(numbersTwo.length);
System.out.println(Arrays.toString(numbersTwo));
}
}
//2d arrays
package com.caleb;
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
//2d arrays
int numbers [][] = new int[2][3];
numbers [0][0] =1;
System.out.println(Arrays.deepToString(numbers));
//method2
int numbersTwo [][] ={{1,3,5},{8,7,6}};
System.out.println(Arrays.deepToString(numbersTwo));
}
}
//CONSTANTS
package com.caleb;
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
final float PI =3.14F;
}
}
//Augmented Operators
package com.caleb;
public class Main {
public static void main(String[] args) {
int x= 1;
int y =++x;
//has different meaning with
int z = x++;
//increasing with two
int p =x+2;
//Augmented operators
p+=2;
System.out.println(y);
System.out.println(z);
System.out.println(p);
}
}
//Casting
package com.caleb;
public class Main {
public static void main(String[] args) {
//Implicit Casting
// byte > short > int >long > float > double
//implicit casting happens whenever we are not going to loose data
//example (when converting a byte to int)
double x = 1.1;
double y = x + 2;
//Explicit casting
//You want to convert a big datatype to a small one
int z = (int) x +2;
System.out.println(z);
System.out.println(y);
//casting incompatible types eg a string ->integer
/*
String H = "1";
int p = Integer.parseInt(H) +2;
System.out.println(p);
*/
String c = "1.1";
Double e = Double.parseDouble(c) +2;
System.out.println(e);
}
}
//Math Operations
package com.caleb;
public class Main {
public static void main(String[] args) {
/*
int result = Math.round(1.1F);
System.out.println(result);
*/
/*
int result = (int) Math.ceil(1.1F);
System.out.println(result);
*/
/*
int result = (int) Math.floor(1.1F);
System.out.println(result);
*/
/*
int result = Math.max(1,2);
System.out.println(result);
*/
//returns a number between 0-1 by default
//mutilply by 100 to return larger numbers
/*
double result = Math.random()*100;
System.out.println(result);
*/
//Rounding off
int result = (int) Math.round(Math.random()*100);
System.out.println(result);
}
}
//Number Format
package com.caleb;
import java.text.NumberFormat;
public class Main {
public static void main(String[] args) {
//Number formatting
// Abstract classes are classes that cannot be instantiated
//Method overriding(one method repeated or implemented mutiple times
/*format currency
NumberFormat currency = NumberFormat.getCurrencyInstance();
String result= currency.format(12345678.91);
System.out.println(result);
*/
//Method chaining
String result = NumberFormat.getPercentInstance().format(0.1);
System.out.println(result);
}
}
//User Input
package com.caleb;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
/*
Scanner scanner = new Scanner(System.in);
//print prints out everything on the same line unlike println which adds an
//extra line
System.out.print("Age:");
//for reading a float i would say next float
byte age = scanner.nextByte();
System.out.println("Your age is" + age + "Years");
*/
/*Reading a String*/
/*
Scanner scanner = new Scanner(System.in);
System.out.print("Name:");
String name = scanner.next();
System.out.println("You are" + name);
*/
/*next line is much more ideal in reading strings unlike next which is
token based and reads the first line*/
Scanner scanner = new Scanner(System.in);
System.out.print("Name:");
//trim is applied to remove the white spaces in the line
String name = scanner.nextLine().trim();
System.out.println("You are" + name);
}
}
//Comparsion Operators
package com.caleb;
public class Main {
public static void main(String[] args) {
//comparison operator
int x=1;
int y =2;
//System.out.println(x<=y);
//inequality operator
System.out.println(x != y);
//others
/* <=2
* */
}
}
//Logical Operators
package com.caleb;
public class Main {
public static void main(String[] args) {
/*
AND Operator
int temperature = 12;
boolean isWarm = temperature> 20 && temperature <30 ;
System.out.println(isWarm);
*/
boolean hasHighIncome = false;
boolean hasLowCredit = true;
boolean hasCriminalRecord = false;
boolean isEligible = (hasHighIncome || hasLowCredit) && !hasCriminalRecord ;
System.out.println(isEligible);
}
}
//If Statement
package com.caleb;
public class Main {
public static void main(String[] args) {
int temp = 2;
if (temp > 30) {
System.out.println("its a hot day");
System.out.println("Drink water");
}
else if(temp > 20 )
//braces are only necessary when i am evaluating multiple statements
System.out.println("Beautiful day");
else
System.out.println("Cold day");
}
}
//Ternary Operator
package com.caleb;
public class Main {
public static void main(String[] args) {
int income = 120_000;
//if i put a variable within a block i only make it
//accessible within that block
//professional way to write code
boolean hasHighIncome = (income > 190_000);
System.out.println(hasHighIncome);
//ternary operator
String className = (income > 100_000) ? "Fist Class" : "Economy Class";
System.out.println(className);
}
}
//Switch Statement
package com.caleb;
public class Main {
public static void main(String[] args) {
String role = "admin";
switch (role){
case "admin":
System.out.println("User is Admin");
break;
case "user":
System.out.println("Normal User");
break;
default:
System.out.println("Unknown");
}
}
}
//If Statements Exercise
package com.caleb;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Number:");
int number= scanner.nextInt();
//always begin with the Most specific condition
if (number % 3 == 0 && number % 5==0 )
System.out.println("Fizz Buzz");
else if (number % 5==0)
System.out.println("Fizz");
else if (number % 3 == 0 )
System.out.println("Buzz");
else
System.out.println(number);
}
}
//for loop
package com.caleb;
public class Main {
public static void main(String[] args) {
for (int i=0; i<5; i++)
System.out.println("CALEB JAVA");
}
}
//While Loop
package com.caleb;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
//while loop is ideal when you do not know how many number of times you will loop through
/*
int i=5;
while (i > 0){
System.out.println("Hello World" + i);
i --;
}
*/
//While loop example
Scanner scanner = new Scanner(System.in);
String input = "";
while(!input.equals("quit")){
System.out.print("Input:");
input = scanner.next().toLowerCase();
System.out.println(input);
}
}
}
//Break and Continue
package com.caleb;
import java.util.Scanner;
public class Main {
//Once Java encounters the Break it leaves the loop and
//does not execute anything else
//Continue statement moves the Control to the beginning of a Loop
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String input = "";
while(!input.equals("quit")){
System.out.print("Input:");
input = scanner.next().toLowerCase();
if(input.equals("pass"))
continue;
if(input.equals("quit"))
break;
System.out.println(input);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment