Just like in C, in Java there is a main function where program execution begins The header is as Date, Integer and String are classes, int and char are primitive types //no setter for aReadOnly, so value won't change after instantiation In a custom exception class, the super constructor must be called with a string
Previous PDF | Next PDF |
[PDF] Creating Your Own Classes
Create and call overloaded methods ○ public - means that our class is accessible to other classes class - this is the keyword used to create a class in Java
[PDF] Building Custom Java Controls - Oracle Help Center
BEA Workshop for WebLogic Platform allows you to create custom controls Custom controls consist of two Java source files: an interface class file and an
[PDF] Customization of Java Library Classes using Type Constraints and
added custom classes is limited to 12 for all but the smallest programs In order to create a customized version of, say, a Hashtable, one could sim-
Solution to the Assignments
Class Assignment 1 Create a class Vehicle The class should have two fields- no_of_seats and no_of_wheels as one of the standard method to implement singletons in Java System out println( "Caught the custom exception : "+e); e
[PDF] Java for WebObjects Developers - Apple Developer
The problem is that to write any code in Java, you must create a new class It is a bit Java does not support operator overloading for custom classes You can
[PDF] Java Classes, Objects, Inheritance, Abstract and Interfaces Recap 2
Recapitulate CSE160: Java Classes, Objects, Inheritance object is created – they initialize objects to reference Custom Exception Class Example 100
[PDF] 1 Java Classes This exercise will give you practice with classes and
In object-oriented programming, we create classes to represent our own custom data types A class consists of various attributes and operations, and individual
[PDF] Analysing Aliasing in Java Applications - DiVA
All programs mostly use objects based on built-in Java classes, indicating incoming references at the same time to any custom object during the execution To achieve this, the model exposes functionality that lets the user create,
[PDF] ELEC 279 Final Exam Solutions - EngLinks
Just like in C, in Java there is a main function where program execution begins The header is as Date, Integer and String are classes, int and char are primitive types //no setter for aReadOnly, so value won't change after instantiation In a custom exception class, the super constructor must be called with a string
[PDF] Discovering WebObjects for HTML - COCKTAIL
Listing 6-1 Main java's constructor - instantiating the user instance variable 80 generating Java class files to add custom logic to enterprise objects □
[PDF] java interpreted language vs compiled
[PDF] java interview cheat sheet
[PDF] java interview notes: 700 java interview questions answered
[PDF] java interview questions
[PDF] java interview questions for 5 years experience pdf
[PDF] java is dynamic
[PDF] java jdk 13.01 download
[PDF] java jdk book
[PDF] java json pdf
[PDF] java lambda exercises online
[PDF] java lang compareto
[PDF] java learning ppt
[PDF] java libraries pdf
[PDF] java loop exercises with solutions pdf
ELEC 279 Final Exam
Solutions
Created by: Carson Cook
Page 2 of 34
Java Basics
Main Function
Just like in C, in Java there is a main function where program execution begins. The header is as follows: This main function will be defined in one and only one class. Even though it is in a class, it is simply the entry point of the program.Packages
In Java, files are separated into different folder directories called packages. In order to access the
classes inside those folders, you must import the directory. For example, this will import the random directory: This will not import sub-directories in random! You either explicitly import all directories, or use * to import them. This would import random as well as all sub-directories:Printing
Printing to the console is as follows:
Classes
Classes are a more modern version of structs in C. With structs, only variables are contained. With classes however, variables can be declared along with functions that are defined. This public static void main(String[] args){ import java.lang.random; import java.lang.random.*; public static void main(String[] args){ System.out.println(//println adds a newline after the supplied text System.out.println(//so this is on the line below HelloSystem.out.println
strings //objects (discussed later) can be turned into output strings by .toString()Page 3 of 34
allows components of a program to be parceled into classes, making it easier to add to and maintain the codebase. The functions defined within a class are called methods. Variables that are not marked static are called instance variables, static variables are called class variables. Methods and variables together are members (again, instance members are non-static, class members are static).Constants can be defined using the final keyword.
Primitive types such as int, float and char are not classes and thus do not have any members.The following is an example of a class:
Question: Which of the following are classes?
Objects
An object is an instance of a class. Just like with C structs, a variable can be made of a class this
is the object. Each object has their own set of instance variables and methods, which can be invoked using the dot operator (just like non-pointer C structs).Constructors
Constructors are used when creating objects. They are prefaced by the new keyword, and then called like how a method is. Constructors will have parameters just like functions (or have none) that are used to initialize instance variables. Unlike methods, constructors do not return any values, and have no return types not even void. If no constructor is defined for a class, thedefault constructor is used; it has no parameters and initializes no variables, it is simply called to
class MyClass { public static int statVar; //class variable public int instVar; //instance variable public static statMethod(){ //class method public void instMethod() { //instance method int, Date, char, Integer, String Date, Integer and String are classes, int and char are primitive types. Convention is classes are named with a capital letter.Page 4 of 34
create the object in memory. If any constructor is defined, the default constructor no longer exists.Question: Find the values.
Static Members
When a member is marked static, there is only one instance of that member. All objects of that class type will share this member. Static variable values are therefore shared across all objects, and static methods cannot reference any non-static members. No object needs to be made to reference these static members; the dot operator can simply be used on the class name. class ThisIsAClass {} //class with no members still has default constructor! class MyClass { public int number; public MyClass(int n) { //constructor it hides the default constructor number = n; //initializes instance variable public static void main(String[] args) { MyClass c = new MyClass(5); //must supply a value for n in constructor ThisIsAClass c2 = new ThisIsAClass(); //uses default constructor //using the above class definitionMyClass mc1 = new MyClass(5);
MyClass mc2 = new MyClass(10);
MyClass mc3 = new MyClass
//what is mc1.number, mc2.number and mc3.number? mc1.number is 5, mc2.number is 10Page 5 of 34
Question: Find the output.
This usually only used to distinguish between instance variables and method parameters of the same name. This keyword can also be used to reference a constructor, this is discussed later in the overloading section. class MyClass { public static int statVar = 0; public int instVar; public static void statMethod(){ statVar++; //okay because statVar is static eference instVar or non-static method public static void main(String[] args){ MyClass.statMethod(); //can reference statics like thisMyClass c = new MyClass();
//c.statVar is 1, as is MyClass.statVarMyClass c1 = new MyClass();
System.out.println(
MyClass c2 = new MyClass();
System.out.println(
MyClass.statMethod();
System.out.println(
System.out.println( //can reference statics using variables, butSystem.out.println(
(Would be on separate lines) 0, 0, 1, 1, 1Page 6 of 34
Static members are class members, not instance members, and therefore are not attached to any objects. Thus, this cannot be used within static methods, or to reference static variables.Question: Find the output.
Assignment Operator with Objects
With objects, the assignment operator sets the reference to be the same. Objects in Java are point to the same place in memory. Therefore, this makes the members of the two objects the same. class MyClass { public int var;MyClass(int var){
this.var = var; //var references the parameter, this.var the instance variable above MyClass.doStuff(this); //this is the object instance of the object that calls myMethod() public static void doStuff(MyClass //use the MyClass defined above, plus the following constructor and method:MyClass(MyClass other) {
this.var = other.var; public void copy() { return new MyClass(this); //now, predict the output of the following code: (ignore functionality of doStuff())MyClass c1 = new MyClass(5);
MyClass c2 = c1.copy();
System.out.println
c2 var = 5MyClass c1 = new MyClass();
MyClass c2 = c1; //now c2 and c1 point to the same place in memory and have the same membersPage 7 of 34
Partly due to this, the equals operator needs to be used with care regarding objects. In Java, the equals operator only compares the memory location. So, if the two objects have the same member values, but point to different locations, they are not considered equal. The .equals() method is available for every class to override and should be implemented and used instead of the equals operator.Question: Find the output.
Encapsulation
Encapsulation is the concept of hiding details of implementation from the client. This may seem like a bad idea, but the point is to be able to use components and classes without knowing how they exactly work. For example, think about driving a car. You know how to get in the vehicle, turn the key and The use of the car is separated from the details of implementation. So, all code you write should keep encapsulation in mind. Will calling code need access to this member? Will this method perform the actions its name would suggest? One simple way to help achieve encAccess Modifier
Keywords can be used to change what code can access each member.Modifier Result
MyClass c1 = new MyClass();
MyClass c2 = c1;
MyClass c3 = new MyClass();
c3.variableMember = c1.variableMember; if (c1 == c2) {System.out.println
if (c3 == c2) {System.out.println
if (c3.equals(c1)) { //assume equals() properly compares every instance variablesSystem.out.println
1 3Page 8 of 34
private Only code within the class can access the member. none Code within the same package can access the member. protected Classes that inherit this class can access the member. public All code can access the member. The above modifiers are sorted with the most restrictive at the top; a modifier allows access to the code as described, plus the access in any modifier above. So, if a method is only used to help another method in the class, it should be marked private. A marked public. Variables are generally always marked private, unless they are constant.Getters and Setters
If variables are marked private, how can the value be retrieved or set (outside of the constructor)? This is where getters and setters come in. These are functions that retrieve and set allows for more control. If a variable should be read only, but set within a constructor, it cannot be a constant. To enforce the read-only property, the variable is marked private and no getter function is made. With setters, the value being used can be checked to ensure it is a proper value before assignment. For example: class MyClass { private int privateVar; //object of MyClass cannot access, only code in MyClass can protected int protVar; //can only be accessed by code in MyClass and by code in classes that //inherit MyClass public String //can be called from anywherePage 9 of 34
Overloading
Function overloading is when two methods have the same name and return type, but with different parameters. This allows the same functionality for different parameter types, without adding another method name. For example: When the method is invoked it is bound with a method of the same return type and parameter list, and that code is executed. If there is no matching method, the compiler attempts to convert types to make the invocation work. This conversion may not be possible for a supplied type, or not be done correctly. Overloading methods are how these issues are avoided. class MyClass { private int aReadOnly; private int aPositiveNum; public MyClass(int aReadOnly, int aPositiveNum) { setAPositiveNum(aPositiveNum); this.aReadOnly = aReadOnly; // aReadOnly is private, but in class code so we can access public setAPositiveNum(int aPositiveNum){ if (aPositiveNum < 0) { //checks for a bad value aPositiveNum = -aPositiveNum; this.aPositiveNum = aPositiveNum; public getAPositiveNum() { return aPositiveNum; } public getAReadOnly() { return aReadOnly; } s class MyClass {MyClass.foo(4);
MyClass.foo
Page 10 of 34
Constructors can be overloaded a similar way. this() is used to call other constructors from a constructor. For example:Question: Predict the output
Copy Constructor
Copy constructors are a special overloaded constructor that take a parameter of the same classtype as itself. The copy constructor then initializes all its instance variables with the same values
as the variables in the passed in object. class MyClass { public int n; public int n2; public MyClass(int n, int n2) { this.n = n; this.n2 = n2; public MyClass(int n) { this(n,5); //calls MyClass(n, 5) public static void main(String[] args) {