Skip to content

Instantly share code, notes, and snippets.

@edisplay
Created August 21, 2016 20:32
Show Gist options
  • Save edisplay/63919062a4aee3522a97327b66c7de45 to your computer and use it in GitHub Desktop.
Save edisplay/63919062a4aee3522a97327b66c7de45 to your computer and use it in GitHub Desktop.
https://repl.it/CpgX/65 created by edisplay
class Main {
public static void main(String[] args) {
//Q1. java-docs
// Find the Java 7 standard library documentation online. What is the URL?
// https://docs.oracle.com/javase/7/docs/api/
//Q2. java-vs-javascript
// Explain in a few sentences what the difference is between Java and JavaScript.
// Java is a compiled language. JavaScript is an interpreted language. Java is a programming language. JavaScript is a scripting language. Java is strictly typed (variables must be declared with type of varible). JavaScript is loosely typed.
// Q3. first-hello-world
// public class Hello {
// public static void main(String[] args) {
// // Outputs "Hello, world!" to the console
// System.out.println("Hello, world!");
// }
// }
// Modify the example program to print "Hello, (your name)!" instead.
// Outputs "Hello, world!" to the console
System.out.println("Hello, Francisco!");
// Insert a second print statement below the first to print "Hello, (name of the person sitting to your left)!"
System.out.println("Hello, Nobody to my left!");
// Q4. print-concatenation
// Given two separate strings "Hello, " and "world!", concatenate them together to print "Hello, world!" to the console.
System.out.println("Hello, " + "World!");
// Or ...
String h = "Hello, ";
String w = "World!";
System.out.println( h + w);
// Q5. hello-world-java-vs-android
// You're probably familiar now with this example, which prints "Hello, world!" to the console:
// public class Hello {
// public static void main(String[] args) {
// System.out.println("Hello, world!");
// }
// }
// Take a look at the following block of Android code, which displays "Hello, world!" on the screen. What differences do you notice from the code above? What looks the same? (Try to name at least 3 similarities and 3 differences!)
// public class HelloActivity extends Activity {
// @Override
// protected void onCreate(Bundle savedInstanceState) {
// super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_hello);
// TextView myTextView = (TextView) findViewById(R.id.my_text_view);
// myTextView.setText("Hello, world!");
// }
// }
// similarities:::
// They are written in Java
// They start with pulic class
// They both use void statatments
// They both use arguments
// differences:::
// pubic statement turns into protected statement
// android sets the screen size/layout/view
// println turns into setText
// Android code is longer thant Java Code
// Q6. java-integer-data-types
// What are the four integer data types? For each one, write down:
// - The min value
// - The max value
// - Reasoning for why you would choose it to represent a given integer
// Answer:
// byte - Min: -128 Max: 127.
// short - Min: -32,768 Max: 32,767.
// int - Min: -2^31 Max: 2^31-1.
// long - min: -2^63 Max: 2^63-1.
// Q7.java-float-data-types
// What are the two floating-point data types? For each one, write down:
// - Its precision
// - Reasoning for why you would choose it to represent a given floating-point number
// float - a real number, single-precision 32-bit floating point.
// double - a real number, double-precision 64-bit floating point.
// You would choose double because it's more precise.
// Q8. string-vs-char
// Explain the difference between char and String. Symble is ''.
// char - a single Unicode character and it's a primity type. Strings - allow char to assigned and used like a primitive data type. It has multiple characters and it's a class/object. Symbol is "".
// Q9. number-data-types
// What happens when you:
// 1. Assign a really big number to a short?
short nShort = 32767;
// an error occurs for incompatible types or too large number
System.out.println(nShort);
// 2. Add a short and an int?
short nS = 25;
int in = 25;
// it adds them with not errors
System.out.println(nS + in);
// 3. Add an int and a double?
double nD = 24.5;
System.out.println(in + nD);
// it adds them with not errors. it automatically casts the int into a double.
// Q10. assigning-variables
// Assign variables for each of the following pieces of information.
// For each one, be sure to choose a suitable data type and a descriptive name!
// Your name.
String name = "Francisco Cruz";
// The first letter of your last name.
char lnInitial = 'C';
// Your favorite food.
String favFood = "Italian";
// The number of days in a week.
byte numDaysWk = 7;
// The number of days in a (non-leap) year.
short numDaysNonLeapYr = 365;
// Whether or not the current year is a leap year.
boolean currentYrIsLeap = false;
// The first three digits of pi.
double shortPi = 3.14;
// How many feet you'd guess there are between the earth and the sun. <!-- end @acxbank -->
double feetInKm = 3280.84;
int earthToSun = 150000000;
double feetEarthToSun = feetInKm * earthToSun;
// Q11. integer-vs-floating-point
// What is the difference between an integer and a floating-point number?
// integer are whole number while floating numbers or doubles are decimals
// Q12. math-api
// Find the Java API for the Math class. Find the method sqrt(). What does it do? What is the data type of the parameter it accepts?
// https://docs.oracle.com/javase/7/docs/api/java/lang/Math.html
// sqrt(double a)
// -- Returns the correctly rounded positive square root of a double value.
// it accepts double data type
// Q13. string-api-intro
// Consider the following string:
// String hannah = "Did Hannah see bees? Hannah did.";
// What is the value displayed by the expression hannah.length()?
String hannah = "Did Hannah see bees? Hannah did.";
System.out.println("The Length is: " + hannah.length());
// What is the value returned by the method call hannah.charAt(12)?
System.out.println("The value returned is: " + hannah.charAt(12));
// Write an expression that refers to the letter b in the string referred to by hannah.
System.out.println("The value returned is: " + hannah.charAt(15));
// Q14. string-substring
// How long is the string returned by the following expression? What is the string?
// "Was it a car or a cat I saw?".substring(9, 12);
String myExp = "Was it a car or a cat I saw?";
System.out.println("Its length is: " + myExp.substring(9, 12).length());
System.out.println("The string is: " + myExp.substring(9, 12));
// Q15. string-api-intro-2
// In the following program, called ComputeResult, what is the value of result after each numbered line executes?
// public class ComputeResult {
// public static void main(String[] args) {
// String original = "software";
// StringBuilder result = new StringBuilder("hi");
// int index = original.indexOf('a');
// /*1*/ result.setCharAt(0, original.charAt(0));
// /*2*/ result.setCharAt(1, original.charAt(original.length()-1));
// /*3*/ result.insert(1, original.charAt(4));
// /*4*/ result.append(original.substring(1,4));
// /*5*/ result.insert(3, (original.substring(index, index+2) + " "));
// System.out.println(result);
// }
// }
String original = "software";
StringBuilder result = new StringBuilder("hi");
int index = original.indexOf('a');
/*1*/ result.setCharAt(0, original.charAt(0)); // value is: si
/*2*/ result.setCharAt(1, original.charAt(original.length()-1)); // value is: se
/*3*/ result.insert(1, original.charAt(4)); // value is: swe
/*4*/ result.append(original.substring(1,4)); // value is: sweoft
/*5*/ result.insert(3, (original.substring(index, index+2) + " ")); // value is: swear oft
System.out.println(result);
// Q16. string-vs-char-experiments
// Play with characters and Strings. What happens if you do the following:
// char c = "a";
// String b = 'foo';
char c = 'a';
char e = 'b';
String b = "foo";
String d = "string";
int strNum = 2;
// What happens if you add, using +, two strings together?
System.out.println(b + d); // answer is foostring which means the strings get concatenated
// Two chars?
System.out.println(c + e); // answer is 195 which means the characters' computed values get added
// A string and a char?
System.out.println(b + c); // answer is fooa which means the strings and characters get concatenated
// A char and a string?
System.out.println(c + b); // answer is afoo which means the strings and characters get concatenated
// Do any other mathematical operations work on strings? <!-- end @acxbank -->
// System.out.println( srtNum + b ); // answer is an error. Numbers cannot be added to strings
System.out.println(b + d); // answer is foostring which means that strings can be concatenated together but not mathematically operated
}
}
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, Francisco!
Hello, Nobody to my left!
Hello, World!
Hello, World!
32767
50
49.5
The Length is: 32
The value returned is: e
The value returned is: b
Its length is: 3
The string is: car
swear oft
foostring
195
fooa
afoo
foostring
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment