Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save rooksoto/682478318350a2fcd756cc70b106aa9d to your computer and use it in GitHub Desktop.
Save rooksoto/682478318350a2fcd756cc70b106aa9d to your computer and use it in GitHub Desktop.
https://repl.it/Cpge/27 created by rooksoto
class Main {
public static void main(String[] args) {
// ***** Q1 *****
// https://docs.oracle.com/javase/7/docs/
// ***** Q2 *****
//The languages are very loosely related, historically, but much different in purpose and implementation. Java is a compiled language, where Javascript is a scripted language. Java is much more verbose, Javascript is, relatively speaking, pared down. Java is statically typed, where Javascript variables are dynamically typed. Java is much more general purpose, Javascript is mainly utilized for web applications (although Node.js makes it possible to use Javascript for server operations and general purpose scripting).
// ***** Q3 *****
System.out.println("Hello, Rafael!");
System.out.println("Hello, Josiel!");
// ***** Q4 *****
String hello = "Hello, ";
String world = "world!";
System.out.println(hello + world);
// ***** Q5 *****
// Similarities -
// 1. Both examples start out by calling and defining a class.
// 2. Both examples have code organized within blocks.
// 3. Both examples use the ";" character to end statments.
// Differences -
// 1. The regular Java example prints to console (using println)
// and the Android example prints to a text view.
// 2. The keyword "extends" is used in the Android example, where it isn't used in this regular Java example.
// 3. The "Strings" class is not called in the Android example, but is necessary in regular Java.
// Questions 6 - 9 completed with Josiel Lopez
// ***** Q6 *****
// Integer types and values -
// byte: -128 to 127
// short: -32,768 to 32,767
// int: -2147483648 to 2147483647
// long: -9.22337203685478E18 to 9.22337203685478E18 -1
// I would use byte and short to save memory, int or long for precision.
// ***** Q7 *****
// Decimal types and values -
// float: 32 bit (single) precision
// double: 64 bit (double) precision, allows many more places after the decimal point than float
// Use float for a smaller decimal to save memory, double for precision.
// ***** Q8 *****
// char vs String
// char is a primitive data type representing a single character, denoted by single quote
// String is a Java class which represents a set of characters (internally, Strings save data as an array of characters)
// ***** Q9 *****
// 1. Returns an error - "integer number too large"
// 2. Automatically casts result as int
// 3. Automatically casts result as dobule
// ***** Q10 *****
// 1. String myName = "Rafael";
// 2. char lastNameInitial = 'S';
// 3. String myFavoriteFood = "Grilled Salmon";
// 4. byte daysInWeek = 7;
// 5. short daysInYear = 365;
// 6. boolean isLeapYear = false;
// 7. float easyPi = 3.14f;
// 8. long distanceToSunInFeet = 500_000_000_000l;
// ***** Q11 *****
// Integers are WHOLE numbers. Floating point numbers are DECIMALS.
// ***** Q12 *****
// The sqrt() method accepts a double as an argument, and returns the square root of the argument.
// ***** Q13 *****
String hannah = "Did Hannah see bees? Hannah did.";
System.out.println(hannah.length()); //Prints "32"
System.out.println(hannah.charAt(12)); //Prints "e"
System.out.println(hannah.charAt(15)); //This refers to (and prints) the letter "b"
// ***** Q14 *****
"Was it a car or a cat I saw?".substring(9, 12);
//This returns the 3-character string "car"
// ***** Q15 *****
String original = "software";
StringBuilder result = new StringBuilder("hi");
int index = original.indexOf('a');
/*1*/ result.setCharAt(0, original.charAt(0));
//result = "si"
/*2*/ result.setCharAt(1, original.charAt(original.length()-1));
//result = "se"
/*3*/ result.insert(1, original.charAt(4));
//result = "swe"
/*4*/ result.append(original.substring(1,4));
//result = "sweoft"
/*5*/ result.insert(3, (original.substring(index, index+2) + " "));
//result = "swear oft"
System.out.println(result);
// ***** Q16 *****
// Note: I did not use StringBuilder for these! I'm aware that StringBuilder provides
// solutions, but I was trying to work strictly within the given instructions.
// 1. Results in error "incompatible types: String cannot be converted to char"
// The fix is to use ' instead of ".
// 2. Results in 3 errors - "unclosed character literal" due to each single quote
// "not a statement" for the intended value "foo"
// The fix is to use " instead of '.
// 3. The strings are concatenated
// 4. Results in errors, no matter how I attempt to add them. One such error was
// "incompatible types: int cannot be converted to String"
// Results in "195" if added within a "System.out.println".
// My only usable solution was as follows:
char firstChar = 'a';
char secondChar = 'b';
System.out.println("" + firstChar + secondChar);
// 5. The String and char are properly concatenated, resulting in a new String
// 6. The char and String are properly concatenated, resulting in a new String
// 7. No. Only the "+" operator works.
}
}
java version "1.8.0_31"
Java(TM) SE Runtime Environment (build 1.8.0_31-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.31-b07, mixed mode)
>>> Hello, Rafael!
Hello, Josiel!
Hello, world!
32
e
b
swear oft
ab
FirstSecond
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment