A comprehensive summary of the course notes for CSC1016S. The following sections of OOP Java are covered:
Chapter One: Intro to Java
Chapter Two: Input and Output
Chapter Three: Flow of Control
Chapter Four: Classes
Chapter Five: Defining Classes
Chapter Six: Arrays
Chapter Seven: Inheritan...
Absolute Java - Walter Savitch - CHAPTER 4 - DEFINING CLASSES I
Alles voor dit studieboek
(10)
Geschreven voor
University of Cape Town (UCT)
Computer Science I (CSC1016S)
Alle documenten voor dit vak (6)
3
beoordelingen
Door: isagarda10 • 3 jaar geleden
Door: zuleighapatel • 3 jaar geleden
Door: rumbivambe1 • 2 jaar geleden
Verkoper
Volgen
stuviasisters
Ontvangen beoordelingen
Voorbeeld van de inhoud
Chapter One
Objects and Methods:
o Object-Oriented Programming (OOP): Programming methodology that views a
program as consisting of objects that interact with one another by means of actions
(called methods)
o Objects of the same kind are said to have the same type or be in the same class
o Other high-level languages have constructs:
• Procedures
• Methods (Java)
• Functions
• Subprograms etc.
o All programming constructs in Java, including methods, are a part of a class
Java Application Programs:
o Two common types of Java programs:
Applications
• A Java application program or “regular” Java program is a class with a method
named main
• When a Java program is run, the run-time system automatically invokes the
main method
• All Java application programs start with the main method
Applets
• A Java program that is meant to run from a Web browser
• Always use a windowing interface
• Can be run from a location on the internet or an applet viewer program
Computer Language Levels:
o High-level language: A language that people can read, write and understand
(A program written in a high-level language must be translated into a language that
can be understood by a computer before it can be run)
o Machine language: A language that a computer can understand
o Low-level language: Machine language or any language similar to machine language
o Compiler: A program that translates a high-level language program into an equivalent
low-level language program
(This translation process is called compiling)
,Byte-Code and Java Virtual Machine:
o Compilers for most programming languages translate high-level programs directly into
the machine language for a particular computer
o Since different computers have different machine languages, a different compiler is
needed for each one
o In contrast, the Java compiler translates Java programs into byte-code, a machine
language for a fictitious computer called the Java Virtual Machine
o Once compiled to byte-code, a Java program can be used on any computer, making it
portable
o Interpreter: Program that translates a program, written in Java byte-code, into the
machine language for a particular computer when a Java program is executed
o The interpreter translates and immediately executes each byte-code instruction
o Translating byte-code into machine code is relatively easy compared to the initial
compilation step
o Most Java programs run using a Just-In-Time (JIT) compiler which compiles a section of
byte-code, at a time, into machine code
Objects and Methods:
o Code: A program or a part of a program
o Source code: A program written in a high-level language such as Java, and the input to
the compiler program
o Object code: The translated low-level program and the output from the compiler
program (e.g. Java byte-code)
o In the case of Java byte-code, the object code is the input to the Java byte-code
interpreter
Class loader:
o Java programs are divided into smaller parts called classes
o Each class definition is in a separate file and compiled separately
o Class loader: A program that connects the byte-code of the classes needed to run a
Java program
o In other programming languages, the corresponding program is called a linker
Compiling a Java Program or Class:
o Each class definition must be in a file whose name is the same as the class name
followed by .java
o Each class is compiled with the command javac followed by the name of the file in
which the class resides
o This result is a byte-code program whose filename is the same as the class name
followed by .class
1.FirstProgram.java
2.javac FirstProgram.java
3.FirstProgram.class
,Running a Java Program:
o A Java program can be given the run command after all its classes have been compiled
o Only runs the class that contains the main method
(The system will automatically load and run the other classes, if any)
o The main method begins with the line:
public static void main(String[ ] args)
o Follow the run command by the name of the class only
Syntax and Semantics:
o Syntax: The arrangement of words and punctuations that are legal in a language
(grammar rules of a language)
o Semantics: The meaning of things written while following the syntax rules of a
language
Error Messages:
o Bug: A mistake in a program
o Debugging: The process of eliminating bugs
o Syntax error: A grammatical mistake in a program
(The compiler can detect these errors, and will output an error message saying what
and where it thinks the error is)
o Run-time error: An error that is not detected until a program is run
(The compiler cannot detect these errors - an error message is only generated after
execution)
o Logic error: A mistake in the underlying algorithm for a program
o (The compiler cannot detect these errors and no error message is generated)
System.out.println:
o Java programs work by having objects perform actions
o System.out: An object used for sending output to the screen
o The actions performed by an object are called methods
o println: The method or action that the System.out object performs
o Invoking/calling a method: When an object performs an action using a method
o Method invocation syntax (in order):
• An object
• A dot (period)
• Method name
• Pair of parentheses
o Arguments: Zero or more pieces of information needed by the method that are placed
inside the parentheses
Using = and +:
o The equal sign (=) is used as the assignment operator
e.g. answer = 2 + 2;
, o The plus sign (+) is used to denote addition or concatenation
(two strings can be connected together)
e.g. System.out.println("2 plus 2 is " + answer);
Identifiers:
o Identifier: The name of a variable or other item (class, method, object, etc.) defined in
a program
o Must not start with a digit
o All characters must be letters, digits, or the underscore symbol
o Theoretically can be of any length
o Java is a case-sensitive language:
Rate, rate, and RATE are the names of three different variables
o Keywords and Reserved words: Identifiers that have a predefined meaning in Java e.g.
public, class, void, static
o Predefined identifiers: Identifiers that are defined in libraries required by the Java
language standard e.g. System, String, println
Naming Conventions:
o Start the names of variables, classes, methods, and objects with a
lowercase/uppercase letter
o Indicate "word" boundaries with an uppercase letter
o Restrict the remaining characters to digits and lowercase letters
e.g. topSpeed, bankRatel, timeOfArrival, FirstProgram, MyClass
Variable declarations:
o Every variable in a Java program must be declared before it is used
o Tells the compiler what kind of data (type) will be stored in the variable
o Give the type of the variable followed by its name and a semicolon
o Variables are typically declared just before they are used or at the start of a block
(indicated by an opening bracket { )
o Basic types in Java are called primitive types:
e.g. int numberOfBeans;
double oneWeight, totalWeight;
Primitive types:
o Boolean
o Char
o Byte
o Short
o Int
o Long
o Float
o Double
Voordelen van het kopen van samenvattingen bij Stuvia op een rij:
Verzekerd van kwaliteit door reviews
Stuvia-klanten hebben meer dan 700.000 samenvattingen beoordeeld. Zo weet je zeker dat je de beste documenten koopt!
Snel en makkelijk kopen
Je betaalt supersnel en eenmalig met iDeal, creditcard of Stuvia-tegoed voor de samenvatting. Zonder lidmaatschap.
Focus op de essentie
Samenvattingen worden geschreven voor en door anderen. Daarom zijn de samenvattingen altijd betrouwbaar en actueel. Zo kom je snel tot de kern!
Veelgestelde vragen
Wat krijg ik als ik dit document koop?
Je krijgt een PDF, die direct beschikbaar is na je aankoop. Het gekochte document is altijd, overal en oneindig toegankelijk via je profiel.
Tevredenheidsgarantie: hoe werkt dat?
Onze tevredenheidsgarantie zorgt ervoor dat je altijd een studiedocument vindt dat goed bij je past. Je vult een formulier in en onze klantenservice regelt de rest.
Van wie koop ik deze samenvatting?
Stuvia is een marktplaats, je koop dit document dus niet van ons, maar van verkoper stuviasisters. Stuvia faciliteert de betaling aan de verkoper.
Zit ik meteen vast aan een abonnement?
Nee, je koopt alleen deze samenvatting voor €6,53. Je zit daarna nergens aan vast.