Summaries from the "Introduction to Programming in Java" textbook to assist in quick studying and revision for tests and exams. Be sure to look at the bundles to buy several chapters at a much lower price. Please note these notes only include summaries for Chapter 2.2 and further summaries will be ...
Introduction
• Referring to a method from another Java file is easy
• Benefits of having methods in different files
o Enables us to reuse code
o Enables modular programming → can develop libraries of static methods for use by any other
program; can independently develop, compile & debug parts of big programs one piece at a time
Using static methods in other programs
• To refer to static method in one class that’s defined in another
o Make both classes accessible to Java (e.g. in same directory in your computer)
o To call a method → prepend its class name & a period separator
• Modular programming = independently develop & debug methods for an application, and use them at any
later time
The public keyword Compile when necessary
• Identifies method as available for use by any • When compiling a program, Java compiles
other program with access to the file everything that needs to be compiled in order to
• Can also identify a method as private (but no run that program
reason to do so at this point) • Useful because all classes that call methods
will use the new version of your code if you fix
Each module is a class any bugs etc.
• Module = all code we keep in a single file
• Each module is a Java class that is kept in a file Multiple main() methods
with the same name as the file but has a .java • More than one class might have a main()
extension method
• For now → each class is a set of static methods • When you type java followed by a class name,
Java transfers control to the machine code
The .class file corresponding to the main() method defined in
that class
• When compiling a program, Java compiler
• Usually have a main() method in every class to
makes a file with the class name + a .class
test & debug its methods
extension that has the program’s code in a
language more suited to the computer Tips
• If you have a .class file, you can use the
• Think of each program you write as
module’s methods in another program without
something you might want to use later
having the source code in the corresponding
• Will allow you to have multiple tools to
.java file (but will be tricky if you find a bug)
use later & save time
1
, Libraries
• Module whose methods are mainly intended for use by many other
programs
• Important characteristic of Java → thousands of libraries have been
predefined for your use
• User-defined libraries = classes that contain a set of related methods for
use by other programs
• No Java library can contain all the methods we might need for a given
computation, so being able to build our own libraries is crucial to
address complex programs
Clients
• Program that calls a given library method
• When a class contains a method that is a client of a method in another
class → first class is a client of the second class
APIs
• Programmers think in terms of a contract between the client & the
implementation that’s a clear specification of what the method is to do
• When writing both clients & implementations, you are making contracts
with yourself → helpful when you are debugging & can reuse code
• API = Application Programming Interface
o Programs written that are clients of built-in Java classes
o Precise specification of signatures of methods available for use
Implementations
• Java code that implements the methods in an API, kept by
convention in a file with the library name & a .java extension
• Every Java program is an implementation of some API (no API is
of use without some implementation)
• Goal when developing an implementation → honour the terms of
the contract
• Separating client code from implementation code gives us
freedom to substitute new & improved implementations
• Limits to the amount of info we can productively include in an API
→ provide to client programmers the info they need & no more
• Implementations change quite frequently
Random numbers
• Code uses particular idioms that convert the random
double values between 0 and 1 that Math.random()
provides to the type of random numbers that we want to
use
• To effectively reuse code that implements these idioms,
we’ll now use the StdRandom library
• Summarize the methods in the StdRandom library with
an API
• Collecting methods with Math.random() to get random
numbers of different types in 1 file → focus on
generating random numbers to this one file instead of
spreading through all programs that use these methods
2
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, Bancontact of creditcard 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 notesfromaCSmajor. Stuvia faciliteert de betaling aan de verkoper.
Zit ik meteen vast aan een abonnement?
Nee, je koopt alleen deze samenvatting voor $2.80. Je zit daarna nergens aan vast.