100% satisfaction guarantee Immediately available after payment Both online and in PDF No strings attached 4.2 TrustPilot
logo-home
Summary

Summary CS114 - 2.2 Libraries and Clients

Rating
-
Sold
-
Pages
7
Uploaded on
24-06-2021
Written in
2021/2022

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 uploaded soon.

Show more Read less









Whoops! We can’t load your doc right now. Try again or contact support.

Document information

Summarized whole book?
No
Which chapters are summarized?
Chapter 2.2 (libraries and clients)
Uploaded on
June 24, 2021
Number of pages
7
Written in
2021/2022
Type
Summary

Subjects

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

Get to know the seller

Seller avatar
Reputation scores are based on the amount of documents a seller has sold for a fee and the reviews they have received for those documents. There are three levels: Bronze, Silver and Gold. The better the reputation, the more your can rely on the quality of the sellers work.
notesfromaCSmajor Stellenbosch University
View profile
Follow You need to be logged in order to follow users or courses
Sold
351
Member since
6 year
Number of followers
278
Documents
0
Last sold
1 month ago
Summaries for Grades 10 - 12 (various subjects)

I am an A-aggregate student, and making summaries is one of my favourite studying techniques. Stuvia gives me the opportunity to share my notes with other learners who may need them!

4,3

68 reviews

5
34
4
25
3
6
2
2
1
1

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their exams and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can immediately select a different document that better matches what you need.

Pay how you prefer, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card or EFT and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Frequently asked questions