Skip to content

Instantly share code, notes, and snippets.

@viveknarang
Last active March 5, 2018 17:20
Show Gist options
  • Save viveknarang/3607dcb80625eeb9fd49180c47d31f96 to your computer and use it in GitHub Desktop.
Save viveknarang/3607dcb80625eeb9fd49180c47d31f96 to your computer and use it in GitHub Desktop.
thisApt = null;
for (int i = 0 ; i < apts.length ; i++) {
if (apts[i].getAptID() == theAptID) {
thisApt = apts[i];
break;
}
}
if (thisApt == null) {
System.out.println("*** The Apartment does not exit! Returning! ***");
return;
}
-------------------------------------------------------------------------------
public class Test {
public static void main(String[] args) {
int[] x = {7, 9, 11, 10};
for (int i = 0; i < x.length ; i++) {
System.out.print(x[i] + " ");
}
System.out.println("");
for (int i = x.length-1; i >= 0 ; i--) {
System.out.print(x[i] + " ");
}
}
}
-----------------------------------------------------------------
public class Test {
public static void main(String[] args) {
int match = 7;
int[] x = {7, 9, 11, 10, 2, 2, 4, 5, 5};
int matchCount = 0;
for (int i = 0; i < x.length ; i++) {
if (x[i] == match) {
matchCount = matchCount + 1;
}
}
System.out.println(matchCount);
}
}
----------------------------------------------------------------------------------------------
public static void main(String[] args) {
// scnr object: used for entering the data
Scanner scnr = new Scanner(System.in);
// A string array for storing the strings that you enter.
String[] words = new String[10];
// This variable is used to point to a cell and track the strings
int i = 0;
// Run this loop until the end; except when you hit break
while (i < words.length) {
// Be user friendly!!!
System.out.println("Please enter a string");
// Get the string input and put it in the ith available cell;
words[i] = scnr.next();
// Again be friendly here to the user!!
System.out.println("Done?");
// Check if the user entered 'y' if yes break and jump out of the loop
if (scnr.next().equals("y")) {
// this is done to stop and jump out of the while loop
break;
}
// You need to increment i for pointing to the next available cell (the next in the sequence)
i++;
}
System.out.println("The value of i at this point is: " + i);
// i is used to print the contents of the string array upto the place where it was filled.
for (int j = 0 ; j <= i ; j++) {
// Print the ith string in the array.
System.out.println(words[j]);
}
}
-----------------------------------------------------------------------------------------------------------------------------
public class Test {
public static void main(String[] args) {
// This loop is there to keep track of rows
for (int i = 0 ; i <= 10 ; i++) {
// This loop is there to add the number of space
for (int j = 0 ; j < i ; j++) {
// Add the spaces before putting the number
System.out.print(" ");
}
// Put the number at the end and jump to the next line.
System.out.println(i);
}
}
}
----------------------------------------------------------------------------------------------------------------------
public class Test {
public static void main(String[] args) {
// An array holding the characters
char[] x = {'A', 'B', 'C'};
// Remember (N) steps; in this case it is 1 & 2 appended to the output strings
for (int i = 1 ; i <= 5 ; i++) {
// This loop is for the (M) substeps i.e appending A,B & C with all the numbers (in this case 1 & 2)
for (int j = 0 ; j <= 2 ; j++) {
// Print out the strings with an added space at the end.
System.out.print("" + i + x[j] + " ");
}
}
}
}
-------------------------------------------------------------------------------------------------------------------------------
// 1 Find the specific theAptID Apartment object and load it into
// thisApt.
// If null, print a message that the apt does not exist and return.
int index = 0;
Apartment foundApt = null;
while (index < apts.length && foundApt == null) {
thisApt = apts[index];
if (thisApt.getAptID() == theAptID) {
foundApt = thisApt;
}
index++;
}
if (foundApt == null) {
System.out.println("*** The Apartment does not exist! Returning! ***");
return;
}
// 2 Verify that thisApt Apt is available.
// If not, print a message that the apt is already rented out and
// return.
if (foundApt.getAptCustomerID() != 0) {
System.out.println("*** The apartment is already rented! Returning! ***");
return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment