Show Menu

Java basic intro Cheat Sheet by

java

Swap

public static void swap(int[] list, int e1, int e2) {
int[] mylist = {1, 2, 3, 4, 5};
int[] mylist2 = new int[9];
int temp;
temp = mylist­[e1];
mylist­2[e2] = mylist­[e1];
mylist­2[e2] = temp;
for (int i : mylist){
System.ou­t.p­rin­tln(i);
}

Lab04 MyDate

public class MyDate {
private int year;
private int month;
private int day;
private int objectNumber;
static int objectCounter;
static String[] strMonths = { "January", "February",
"March", "April", "May", "June", "July", "August", "September", "October", "November",
"December"};
MyDate(){
this.setDate(1900,1,1);
objectCounter++;
objectNumber = objectCounter;
}
MyDate(int aYear,int aMonth,int aDay){
this.setDate(aYear, aMonth, aDay);
objectCounter++;
objectNumber = objectCounter;
}

public int getObjectNumber() {
return objectNumber;
}
public void setDate(int aYear,int aMonth,int aDay) {
this.setYear(aYear);
this.setMonth(aMonth);
this.setDay(aDay);
}
public void setYear(int aYear) {
year = aYear;
}
public void setMonth(int aMonth) {
month = aMonth;
}
public void setDay(int aDay) {
day = aDay;
}
public int getYear() {
return year;
}
public int getMonth() {
return month;
}
public int getDay() {
return day;
}
public static int yearDiff (MyDate start, MyDate end) {
int amonth = start.month;
int ayear = start.year;
int counter = 0;
int result = 0;
if (end.year >= start.year) {
boolean process = true;
while(process == true) {
if(amonth == 12) {
amonth = 1;
ayear++;
counter++;
}
else if (amonth == end.month && ayear == end.year) {
process = false;
}
else {
amonth++;
counter++;
}
}
if (start.day > end.day) {
counter--;
}
result = counter / 12;
}
else {
result = -1;
}
return result;
}

@Override
public String toString() {
return day +" "+ strMonths[month - 1] +" "+ year;
}

MyDate nextDay() {
if (this.getMonth() == 12) {
this.setDate(this.getYear() + 1,1,1);
}
else {
if(this.getMonth() == 4 || this.getMonth() == 6 || this.getMonth() == 9 || this.getMonth() == 11) {
if(this.getDay() == 30) {
this.setMonth(this.getMonth() + 1);
this.setDay(1);
}
else {
this.setDay(this.getDay() + 1);
}
}
else if(this.getMonth() != 2){
if(this.getDay() == 31) {
this.setDay(1);
this.setMonth(this.getMonth() + 1);
}
else {
this.setDay(this.getDay() + 1);
}
}
else {
if (isLeapYear(this.getYear()) == true && this.getDay() == 29) {
this.setMonth(this.getMonth() + 1);
this.setDay(1);
}
else if (isLeapYear(this.getYear()) == false && this.getDay() == 28) {
this.setMonth(this.getMonth() + 1);
this.setDay(1);
}
else {
this.setDay(this.getDay() + 1);
}
}
}
return this;
}

MyDate nextMonth(){
if (this.getMonth() == 12) {
this.setMonth(1);
this.setYear(this.getYear() + 1);
}
else if (this.getMonth() == 3 || this.getMonth() == 5 || this.getMonth() == 8 || this.getMonth() == 10) {
if(this.getDay() == 31) {
this.setDate(this.getYear(), this.getMonth() + 1, 30);
}
else {
this.setMonth(this.getMonth() + 1);
}
}
else if (this.getMonth() == 1){
if (isLeapYear(this.getYear()) == true && this.getDay() >= 29) {
this.setDate(this.getYear(),this.getMonth() + 1, 29);
}
else {
this.setDate(this.getYear(),this.getMonth() + 1, 28);
}
}
else{
this.setMonth(this.getMonth() + 1);
}
return this;
}

MyDate nextYear() {
if (isLeapYear(this.getYear()) == true && this.getDay() == 29 && this.getMonth() == 2) {
this.setDate(this.getYear() + 1, 2 , 28);
}
else {
this.setYear(this.getYear() + 1);
}
return this;
}
MyDate previousDay() {
if (this.getMonth() == 1 && this.getDay() == 1) {
this.setYear(this.getYear() - 1);
this.setMonth(12);
this.setDay(31);
}
else {
if(this.getMonth() == 5 || this.getMonth() == 7 || this.getMonth() == 10 || this.getMonth() == 12) {
if (this.getDay() == 1) {
this.setMonth(this.getMonth() - 1);
this.setDay(30);
}
else {
this.setDay(this.getDay() - 1);
}
}
else if (this.getMonth() != 3) {
if (this.getDay() == 1) {
this.setDate(this.getYear(), this.getMonth() - 1, 31);
}
else {
this.setDay(this.getDay() - 1);
}
}
else {
if(isLeapYear(this.getYear()) == true && this.getDay() == 1){
this.setDate(this.getYear(),this.getMonth() - 1, 29);
}
else if (this.getDay() == 1) {
this.setDate(this.getYear(), this.getMonth() - 1, 28);
}
else {
this.setDay(this.getDay() - 1);
}
}
}
return this;
}

MyDate previousMonth() {
if (this.getMonth() == 1) {
this.setMonth(12);
this.setYear(this.getYear() - 1);
}
else if (this.getMonth() == 5 || this.getMonth() == 7 || this.getMonth() == 10 || this.getMonth() == 12) {
if(this.getDay() == 31) {
this.setDate(this.getYear(), this.getMonth() - 1, 30);
}
else {
this.setMonth(this.getMonth() - 1);
}
}
else if (this.getMonth() == 3){
if (isLeapYear(this.getYear()) == true && this.getDay() >= 29) {
this.setDate(this.getYear(),this.getMonth() - 1, 29);
}
else {
this.setDate(this.getYear(),this.getMonth() - 1, 28);
}
}
else{
this.setMonth(this.getMonth() - 1);
}
return this;
}

MyDate previousYear() {
if (isLeapYear(this.getYear()) == true && this.getDay() == 29 && this.getMonth() == 2) {
this.setDate(this.getYear() - 1, 2 , 28);
}
else {
this.setYear(this.getYear() -1);
}
return this;
}
static boolean isLeapYear(int Year) {
if (Year % 4 != 0) {
return false;
}
else if (Year % 100 != 0) {
return true;
}
else if (Year % 400 != 0) {
return false;
}
else {
return true;
}
}
}
 

Range of Data Types

Byte = – 128 to 127 [-2^7 to 2^7 - 1]
Short = – 32,768 to 32,767 [-2^15 to 2^15 - 1]
Int = –2,147­,48­3,648 to 2,147,­483,647 [-2^31 to 2^31 - 1]
Long = –9,223­,37­2,0­36,­854­,77­5,808 to 9 ,223,3­72,­036­,85­4,7­75,807
= [-2^63 to 2^63 -1]

Lab04 Person

import java.time.LocalDate;

public class Person {
private String firstname;
private String lastname;
private MyDate birthday = new MyDate();

Person(String aFirstname, String aLastname){
firstname = aFirstname;
lastname = aLastname;
}

Person(String aFirstname, String aLastname, int aYear, int aMonth, int aDay){
firstname = aFirstname;
lastname = aLastname;
birthday.setDate(aYear, aMonth, aDay);
}
public int getAge(MyDate aDate) {
int age = MyDate.yearDiff(birthday, aDate);
return age;
}
public boolean isEligible(MyDate elecDate) {
if (MyDate.yearDiff(this.birthday, elecDate) >= 18) {
return true;
}
else {
return false;
}
}
public void printPersonInfo() {
System.out.println("Person: "+firstname+" "+lastname);
System.out.println("Birthday: "+birthday.getDay()+" "+MyDate.strMonths[birthday.getMonth() - 1]+" "+birthday.getYear());
}

}

Lab04

import java.util.Scanner;
public class ElectionTester {
public static void main(String[] args) {
MyDate election = new MyDate(2019, 3, 24);
Person a = new Person("Lalisa", "Manoban", 1997, 3, 27);
printPersonElectionInfo(a, election);
Person b = new Person("Nuda", "Inter", 2012, 1, 16);
printPersonElectionInfo(b, election);
Person c = new Person("Hallo", "World",1998,2,1);
printPersonElectionInfo(c, election); //
boolean processing = true;
while(processing == true) {
String firstname;
String lastname;
int year;
int month;
int day;
Scanner in = new Scanner(System.in);
System.out.print("Enter firstname or type 'q' to exit:");
String dataA = in.nextLine();
if(dataA.equals("q") == true) {
processing = false;
break;
}
else {
firstname = dataA;
Scanner in1 = new Scanner(System.in);
System.out.print("Enter lastname:");
lastname = in1.nextLine();
Scanner in3 = new Scanner(System.in);
System.out.print("Enter year of birthday: ");
year = in3.nextInt();
Scanner in4 = new Scanner(System.in);
System.out.print("Enter month of birthday: ");
month = in4.nextInt();
Scanner in5 = new Scanner(System.in);
System.out.print("Enter day of birthday: ");
day = in5.nextInt();
Person data = new Person(firstname,lastname,year,month,day);
printPersonElectionInfo(data, election);
}
}
System.out.println("Exit the program. Thank you.");
}
public static void printPersonElectionInfo(Person p, MyDate election) {
p.printPersonInfo();
System.out.println("Age: " + p.getAge(election));
if(p.isEligible(election))
System.out.println("This person is eligible to vote.");
else
System.out.println("This person is NOT eligible to vote");

System.out.println("-----------------------------------");
}
}

Code

//
=
/
/t
=
tap
/*
=
comment

Loop

while loop
int x = 1;

while (x < 10) {
System.ou­t.p­rin­tln(x);
x++;

for loop
for (int a = 0; a < 10; a++){
System.ou­t.p­rin­tln(a);
Ench­anted for loop
int number[] = { 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};

for (int a: number) {
System.ou­t.p­rin­tln(a);
whil­e..do loop
int x = 1;

do{
System.ou­t.p­rintln( x );
x++;
}
while( x < 10 );

Download the Java basic intro Cheat Sheet

6 Pages
//media.cheatography.com/storage/thumb/phon_java-basic-intro.750.jpg

PDF (recommended)

Alternative Downloads

Share This Cheat Sheet!

 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          Selenium WebDriver Cheat Sheet Cheat Sheet
          Spring Framework 4 Cheat Sheet

          More Cheat Sheets by phon

          Hallo? Cheat Sheet