Python supports multiple styles of programming as well as language unique features Object oriented programming
An expert can write TRULY UNREADABLE code that is VERY SHORT and POWERFUL. You can build that expertise up from the core syntax you have.
Python:
Java:
public class Hello {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}Difference between System.out.println() and System.out.print()
The main method looks like this:
public is an access modifier (private is another option)static – the method belongs to the class itself rather than to an instance of that classvoid is what the method returns (nothing)String[] args is the parameter, an array of StringModify your main method in your HelloWorld class to assign the string literal "Hello World!" to a String variable name, and then print out that variable instead.
Questions:
Can you use both double and single strings to create the variable?
Does concatenation with the + operator work?
Can we concatenate (glue together) String, char and int (or a combination of these)?
Declare any time before use, and strongly-typed
booleanchar (unicode) – single quotesint
short and byte for smaller intslong for longer intsdouble
float for shorter floatsPrimitive type names are lower-case
These act like c++ variables
All operators as you would expect:
+ * - / %
// in Java. Division type will be based on input typesint divided by an int is an int< > <= >=
== !=
&& || !
variable names often camelCase
Java requires a lot of code to express a simple concept.
You need to be much more explicit when writing Java than Python (more words to type in Java)
Comments are extra important
Answer the in-class exercise on gradescope
Create a Java application that determines if an integer is odd or even
Submit a java class called OddEven to Gradescope that contains two public methods:
isOdd(int number) takes an integer as argument and returns a booleanisEven(int number) takes an integer as argument and returns a booleanYou have 10 minutes to complete the quiz
Enter one line number per response box.
public class OddEven {
public static boolean isOdd(int number) {
boolean result = number % 2 == 1;
return result;
}
public static boolean isEven(int number) {
return number % 2 == 0;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int number = 3;
if (isOdd(number)) {
System.out.println("Number is odd");
}
if (isEven(number)) {
System.out.println("Number is even");
} else {
System.out.println("Number is not even");
}
}
}Syntax:
Submit a java class called Average to Gradescope that contains one public method:
average(int[] numbers) takes an array of integers as argument and returns a double representing the average of all values in the array (sum values, divide by total number of values)