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 ...
Chapter 2.2 (libraries and clients)
June 24, 2021
7
2021/2022
Summary
Subjects
cs114
libraries
clients
functions
summaries
java
comp sci
coding
computer science
study
test
revision
textbook
libraries and clients
rw114
notes
exam
a2
a3
a1
Connected book
Book Title:
Author(s):
Edition:
ISBN:
Edition:
More summaries for
Programming in Java
CS114 - 2.1 Defining Functions
CS114 - 1.6 Case Study Random Web Surfer
All for this textbook (4)
Written for
Stellenbosch University (SUN)
Computer Science 114 (CS114)
All documents for this subject (5)
Seller
Follow
notesfromaCSmajor
Reviews received
Content preview
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
The benefits of buying summaries with Stuvia:
Guaranteed quality through customer reviews
Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.
Quick and easy check-out
You can quickly pay through credit card or Stuvia-credit for the summaries. There is no membership needed.
Focus on what matters
Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!
Frequently asked questions
What do I get when I buy this document?
You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.
Satisfaction guarantee: how does it work?
Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.
Who am I buying these notes from?
Stuvia is a marketplace, so you are not buying this document from us, but from seller notesfromaCSmajor. Stuvia facilitates payment to the seller.
Will I be stuck with a subscription?
No, you only buy these notes for $2.75. You're not tied to anything after your purchase.