Intro To Java Flashcards
How do we execute a Java Program? Let’s say we have a program called Hello.java
Go to the command line, and navigate to your directory. You will then type
javac Hello.java java Hello
If you have a file in an IDE and want to run it using the command line, will we get any errors?
An IDE adds many files into the folder. If you try and run it within the folder that the IDE creates, it won’t work because of the package that the IDE creates.
You should always check your code in the command line.
What are the two ways of adding comments in Java?
Multi-line commenting is done with /* */
Same line commenting is done with //
How do we run the documentation in Java? Let’s say that we have a program called Hello.java
Like always, we first start by compiling:
javac Hello.java javadoc Hello.java
What are the primitive data types in Java?
boolean
char
byte
short (signed integer)
int (signed integer)
long (signed integer)
float (floating point)
double (floating point)
How is an integer declared in Java?
int exampleVariable = 409;
What is the difference between automatic type casting (widening) and explicit type casting (narrowing)?
Widening(Automatic): assigns a smaller type to a larger type (int -> double)
int intNum = 25; double doubNum = intNum; //auto cast
Narrowing (Explicit): assign a larger type to a smaller type. (double -> int)
double doubNum = 25.5; int intNum = (int)doubNum; //explicit cast
How do we print out values in Java?
class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World!"); } }
What is the main() method?
Each class has its own main. Main can be thought of as an entry point.
Execution starts with the main. But just because a class doesn’t have a main doesn’t mean it won’t be used.
For assignments in this class, it is recommended that you use the main method.
What is the problem with this code?
public class Creature { public static void main(String[] args){ System.out.println("This is a placeholder for Creature"+ args [0]); } }
It can only handle the case that the user only enters in one argument. For example
java Creature cat
For the following code, how would we throw an exception?
public class Creature { public static void main(String[] args){ if(args.length == 0){ \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_ } else { System.out.println("This is a placeholder for Creature" + args[0]) } } }
public class Creature { public static void main(String[] args){ if(args.length == 0){ throw new IllegalArgumentException("You must supply a command-line argument"); } else { System.out.println("This is a placeholder for Creature" + args[0]) } } }
What is another name for data members and member functions?
Another name for data members is fields
Another name for member functions is methods
In Java is String a primitive function?
No, String is a class. Classes in Java start with capital letters.
What are some Non-Primitive Data Types in Java?
String
Array
List
Set
Stack
In Java, what convention do we use to name Classes and how does that differ from naming methods and variables?
In Java, we captialize the name of Classes, but we name methods and variables with lowercase.
We also use camel case for both.
ClassName
methodName
For the following access modifiers, what are their levels of visibility?
public
protected
default
private
public: Class, Package, Subclass, World
protected: Class, Package, Subclass, World
default: Class, Package
private: Class
What is inclusion?
Inclusion is when you put one class inside another class.
(For now avoid this)
Where are getter and setter functions placed (public or private?). What are the naming conventions for getter and setter functions?
Getter and setter functions are often placed in public.
The naming convention is to name getters with get in front and setters with set in front.
Example: public class Creature { public static void main(String[] args){ Animal myPet = new Animal(); mypet.setAnimalType("mouse"); } } class Animal { private String animalType; public void setAnimalType(String arg) { animalType = arg; } public String getAnimalType(){ return animalType; } }
Consider the following code Fill in the blank so that we can retrieve the animal kingdom:
public class Creature { public static void main(String[] args){ Animal myPet = new Animal(); mypet.setAnimalType("mouse"); } System.out.print(\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_); } class Animal { private String animalType; public void setAnimalType(String arg) { animalType = arg; } public String getAnimalType(){ return animalType; } public static String getKingdom(){ return kingdom; } }
public class Creature { public static void main(String[] args){ Animal myPet = new Animal(); mypet.setAnimalType("mouse"); } System.out.print("This animal belongs to "+ Animal.getKingdom()); } class Animal { private String animalType; public void setAnimalType(String arg) { animalType = arg; } public String getAnimalType(){ return animalType; } public static String getKingdom(){ return kingdom; } }
Note that we set the value of kingdom to be the same for all animals.
What is UML. What does it help us do?
UML stands for Unified Modeling Language. It provides a standard way to visualize elements of a system design.
It allows the visualization of structures, behaviours and interactions.
Assume that we have a simple class named Person. How would we define private members name (a string) and age, and age (an integer).
public class Person { // Fields (data members) - private by default private String name; private int age; ...
In the following sample code, write code for a consructor that takes in two arguments, name and age, and then assigns them to the members name and age.
~~~
public class Person {
// Fields (data members) - private by default
private String name;
private int age;
…
~~~
public class Person { // Fields (data members) - private by default private String name; private int age; // Constructors - initialize the object public Person(String name, int age) { this.name = name; this.age = age; } ...
Write a getter function called getName which retrieves the value for a variable called “name”. Will this method be public or private?
The method will be public:
public String getName() { return name; }
Write a setter method for updating the value of the private field ‘name’. In setter functions why is the “void” required?
The void keyword is used because it doesn’t return any value.
~~~
public void setName(String name) {
this.name = name;
}
~~~