Academia.edu no longer supports Internet Explorer.
To browse Academia.edu and the wider internet faster and more securely, please take a few seconds to upgrade your browser.
2017
https://doi.org/10.1007/978-3-319-89491-1…
514 pages
1 file
Fundamentals of Java Programming By Mitsunori Ogihara































































































































































































































![Arrays are objects, so before accessing their individual elements, they must be instantiated. Ar
array is instantiated with its number of elements. We call the number of elements in an array the
length or size of the array. The length specification appears inside a pair of square brackets [] afte:
the type specification of the individual elements. Examples of the length specification are showr
next. Line 4 instantiates a 10-element array of int values, Line 5 instantiates a 17-element array o:
String values, and Line 6 instantiates a 40-element array of double values.
© Springer Nature Switzerland AG 2018
M. Ogihara, Fundamentals of Java Programming,
https://doi.org/10.1007/978-3-319-89491-1 12](https://figures.academia-assets.com/63186888/table_161.jpg)
![AN eS MDE een eres NING ewan ee aearee ee AAMETRuYE Oras entre Nn! MURS TT SENOS eNN eens Sn tne ae Siew ERMA Inrsn ner Socwaenrcnes YemrranGn go nneen enrvorinee Seietnwas smo eh nse setae cane pune
Here is the first part of the source code, where the program receives the scores. The program
leclares an int array counts and instantiates it as an 11-element array (Line 8). The program asks
he user to start entering numbers and enter CTRL-D to report the end of input (Lines 10 and 11).
[o receive input, the program uses a while-loop with keyboard.hasNext () as the continuation
‘ondition. As we saw in Chap. 11, keyboard.hasNext () returns false only when the user
nters CTRL-D at the start of the input sequence. In the loop-body, the program receives a new score
ising keyboard.nextInt (), and then stores it in the variable score (Line 14). The program
letermines the index of the bin in the manner we have discussed, and then stores it in the variable
o9osition (Line 15). The program then increases counts[ position ] by 1 (Line 16).
The program must produce two types of output from the counts thus obtained, a numerical output
and a bar-histogram. In both outputs, the program uses the numbers 0, 10, ..., 90, 100 as the
representatives of the bins. These representative numbers are equal to ten times the indexes to the
bins. In the bar-histogram output, the program prints a horizontal line of hash marks for each bin. The
lengths of the lines are equal to the counts. The histogram can be generated using a double for-loop.](https://figures.academia-assets.com/63186888/table_162.jpg)











![Listing 12.12 A program that counts the occurrences of letters (part 3). The part responsible for printing the result.
To present the result, the program prints the input line (Line 22), the lowercase version (Line
23), and a header line (Line 24). The program then uses a for-loop that iterates over the sequence
0, ..., 25 with a variable named index (Line 25). The character represented by index is
obtained by converting the relative index to its absolute index with the addition of a’ , and then casting
of (char) on the absolute index. In other words, (char) ( ‘a’ + index ) produces the letter
(Line 28). The count corresponding to the letter is count [ index ] (Line 28). The program prints
the letter and the count using printf with the format of "6c: %2d%n" (Line 27). The format
instructs to allocate six character spaces to the letter and two character spaces to the count.](https://figures.academia-assets.com/63186888/figure_068.jpg)



![Since the value of flags[ n ] does not change from false to true, the loop can be
terminated as soon as the value changes to false:](https://figures.academia-assets.com/63186888/figure_069.jpg)




![Listing 12.15 A program that receives names and scores and performs search (part 1).
To retrieve scores, the program receives two double values from the user, and stores them in
double variables, Low and high (Lines 27-29). The program uses a variable named count to
record the number of scores that have passed the test (Line 31). The program uses a for-loop that iter-
ates over the sequence of valid index values with a variable named i (Line 32). For each index value of
i, the program stores the valueof ( scores[ i ] >= low && scores[ i ] <= high )
inflags[ i ] (Line 34), and then, if the value obtained is true (Line 35), increases the value of
count by | (Line 37). The program announces the value of count after completing the recalculation
of the elements in flags (Line 40). To report which scores are in the range, the program uses
a for-loop again with the iteration variable i (Line 42). If flags[ i ] has the value of true
(Line 44), the program prints i, names[ i ], and scores[ i ] in one line using the format
"$3d %10s %6.2£%n". This means that three character spaces are allocated for i, ten character](https://figures.academia-assets.com/63186888/figure_071.jpg)




























![The program FixedSizeUnsorted is an example of arrays with capacity and size. The
application receives an initial set of String data from the user, and then interacts with the user
to make modifications and perform examinations on the data. The operations that are available on the
data are adding elements in succession, removing one element, viewing the collection, and searching
for all the elements matching a key specified by the user. The program uses four global variables,
String[] theNames,int capacity,int size,andScanner keyboard (Lines 6-10).
The variables represent the array, the capacity of the array, the size of the array, and the keyboard
with which to receive input from the user. The method main consists of two method calls: setup
(Line 12), for setting up the initial array, and action (Line 13), for performing the interaction with
the neer](https://figures.academia-assets.com/63186888/figure_086.jpg)











![Listing 14.3 A program that computing partial sums using a two-dimensional array (part 3). The method print
The method print is for printing the data of a two-dimensional array (Line 38). The program uses
a double for-loop to generate row-column index pairs with variables rowPos and col Pos (Lines 40
and 42), and then prints twoD[ rowPos ][ colPos ] using the format 5d (Line 44). At the
end of each row, the method prints the newline (Line 46).](https://figures.academia-assets.com/63186888/figure_089.jpg)
![program instantiates a two-dimensional array as oneD. length by oneD. length (Line 24). The
method then executes a double for-loop to fill this array. The external for-loop generates the row
index with a variable named rowPos (Line 25). For each value of rowPos, the method stores
oneD[ rowPos ] in twoD[ rowPos ][ rowPos ] (Line 27), and then using an internal
for-loop, generates the column index values rowPos + 1, ..., oneD.length - 1 witha
variable named colPos (Line 28), and stores twoD[ rowPos ][ colPos - 1 ] +oneD
[ colPos ] intwoD[ rowPos ][ colPos ] (Lines 30 and 31). After the double for-loop,
the method returns twoD (Line 34).
Listing 14.2 A program that computing partial sums using a two-dimensional array (part 2). The method
partialsAll](https://figures.academia-assets.com/63186888/figure_090.jpg)

![The next is the part for receiving the names and scores. The program uses a for-loop that
iterates over the sequence 0, ..., nPeople - 1 with a variable named index. At each
round, the program receives the name of the person (Lines 17 and 18), receives the number of
scores for that person, stores the number in a variable named size (Lines 19 and 20), instantiates
data[ index ] withnew double[ size ] (Line 21), and then uses an interior for-loop to
generate column indexes 0, ..., Size - 1 witha variable named col (Line 23) to receive the
elements data[ index ][ 0], ..., data[ index ][ size - 1 ] (Line 25).
Listing 14.6 A program that receives the names and scores for a number of students and reports the averages of th
scores (part 2). The part responsible for receiving the names and the scores](https://figures.academia-assets.com/63186888/figure_091.jpg)
![Listing 14.7 A program that receives the names and scores for a number of students and reports the averages of the
scores (part 3). The part responsible for computing the averages
The calculation of the averages requires a double for-loop. The external for-loop iterates over the
sequence 0, ..., nPeople - 1 witha variable named index (Line 29). The internal for-loop
iterates over the sequence 0, ..., data[ index ].length - 1 witha variable named col
(Line 31). The program stores the total of the scores appearing in that row in average[ index ]
(Line 33). Then, with the division by data[ index ].length, the program scales the total
stored inaverage[ index ] to the average.](https://figures.academia-assets.com/63186888/figure_092.jpg)







![After these array instantiations, the method examines the files appearing in the list. Using
a for-loop, the method generates the index sequence 0, ..., list.length - 1 with
a variable named i. At each round, the method executes the five tests, canExecute,
canRead, canRead, isDirectory, and isFile, in this order, and stores the results in
flags[ i][0], ..., flags[ i ][ 4 ] (Lines 14-18). The program also obtains
the file length and the file name using the methods length and getName, and stores the returned
values in their respective arrays, size and name.
Listing 15.5 A program that demonstrates the use of File methods for examining properties of files (part 1). The
part responsible for taking the inventory](https://figures.academia-assets.com/63186888/figure_097.jpg)
























![Here is a code for ArrayWithCapacity. The global variables are now private instance variables
(Lines 4 and 5). They are int variables, size and capacity, and a String[] variable,
theNames. The constructor takes an int value, capacity, as the formal parameter (Line 7).
The constructor saves the value of the parameter to the instance variable, capacity. Here we use
this. for distinction (Line 9). The constructor then instantiates the array using the capacity value as
the length (Line 10) and assigns 0 to size (Line 11). The getters get Capacity (Lines 14-17) and
getSize (Lines 19-22) return the values of capacity and size.
e We will add new methods, get Capacity and get Size, as getters.](https://figures.academia-assets.com/63186888/table_224.jpg)












![Listing 17.15 The code for PizzaComplexCollection (part 1). The header, the constants, the instance variables
and the constructor
In PizzaComplexCollection, the type of the pizza array is PizzaComplexIt [] (Line 5).
The class defines two delimiters to be used as a parameter for the method encodeIngredients
of PizzaComplexInt. The delimiters are called SEPARATOR and COMMA. SEPARATOR is used
when recording the data in a file and its value is "\t" (Line 8). COMMA is used when printing the
ingredients on the screen, and its value is ", " (Line 9). As before, there are two constructors, one
that receives a file path (Line 11) and the other that takes a File object (Line 15). The order between
the two is deliberately switched in this version, to emphasize that the order of appearance of the two
constructors can be arbitrary.](https://figures.academia-assets.com/63186888/table_233.jpg)




![17.5 Interface Comparable
By using Integer instead, we can make max assume the role of the boolean variable.
We use the disjunction || in the first condition inside the while-loop to truncate the conditional
evaluation. At the end of the loop-body, we set the value of noNumbersYet to false, so the
second time around, the second condition, max < number, is tested.
Runeneo Tntaqgqaenr inctead we ran make may acame the role af the han] aan variahle](https://figures.academia-assets.com/63186888/table_238.jpg)



























































These lecture notes are designed for use in the first year Computer Science modules at the University of KwaZulu-Natal. They provide an introduction to problem solving, programming, and the Java language. They are not intended to be complete in themselves but serve as a complement to the formal lectures, and students are urged to make use of the books referenced in addition to these notes.
THE ART AND SCIENCE OF JAVA PROGRAMMING
ACM SIGCSE Bulletin, 1998
The goal of this working group was to collect, evaluate, and foster the development of resources to serve as components of both new and revised traditional courses that emphasize object-oriented software development using Java. These courses could, for example, integrate Internet-based distributed programming, concurrency, database programming, graphics and visualization, human interface design and object-oriented development. They could therefore also be suitable as capstone courses in computer science. The focus of the working group was on tools and techniques, including demonstrations, projects, syllabi, and pedagogical patterns. The working group members are coordinating the development of a Web site (sol.pace.edu/iticse98) devoted to sharing such tools and techniques among educators.
2018
Java is currently one of the most influential programming languages. It all started in 1990, when an American company that was leading the revolution in the computer industry decided to gather its best engineers together to design and develop a product that would allow them to become an important player in the new emerging Internet world. Among those engineers was James Arthur Gosling, a Canadian computer scientist who is recognized as the “father” of the Java programming language. It would take five years of design, programming, and one rename (from Oak to Java because of trademark issues), but finally in 1996, Java 1.0 was released for Linux, Solaris, macOS, and Windows.
The general format of do..while is:
OOP concepts-Data abstraction-encapsulation-inheritance-benefits of inheritancepolymorphism-classes and objects-procedural and object oriented programming paradigm. Java programming -History of java-comments data types-variables-constants-scope and life time of variables-operators-operator hierarchy-expressions-type conversion and castingenumerated types-control flowblock scope-conditional statements-loops-break and continue statements-simple java stand alone programs-arrays-console input and output-formatting output-constructors-methods-parameter passing-static fields and methods-access control-this reference-overloading methods and constructors-recursion-garbage collection-building stringsexploring string class.
Foreword When planning a Java programming course in a school or even in a business context, the reduced number of hours available often has to be taken into account. A program that includes all aspects of the language is not feasible (for a much more extensive approach, from the syntactic point of view and for the topics dealt with, see note 3), but rather topics must be chosen with the aim of involving and also entertaining users, and hopefully encouraging to study the subject in greater depth in the future. Here we put forward a"possible method" (one of many) that has been repeatedly tested on many students. The method includes the advice to students to use the help and online help often (instructions on how to do this will be provided) to encourage autonomy in those who write programs from the outset. At the end, you will have learned to write some programs and be ready to start creating other projects (not exclusively in Java). The cognitive structures transmitted during the course are first and foremost linguistic structures. Noam Chomsky's Generative Transformational Grammar is the basic text from which the initial idea of the method started. In a 2001 interview, Dijkstra stated a desire for "elegance", whereby the correct approach would be to process thoughts mentally, rather than attempt to render them until they are complete. Here we can add that the constant correct use of formal language can contribute to the correct formulation of thoughts. If then, as in the case of Java, we are dealing with an object-oriented language, the syntactic structure contributes greatly to the organisation of thought. A linguistic model must therefore be proposed at the same time as the problem to be solved is presented, whether the problem is mathematical or otherwise. It is preferable to first analyse the algorithm in a discussion and then immediately formalise it in Java: no metalanguages or intermediate formal languages are needed as it could be misleading to isolate the problem from its final "linguistic solution". Finally, structured and functioning examples containing different linguistic constructs in relation to each other are analysed, and in this way more information is communicated to users than when single separate syntactic examples are used. Introduction From a technical point of view, every program in Java can be described as follows • It consists essentially of classes: the class is the linguistic unit that contains data (variables or properties) and operations (methods) on the data and it is recognisable because its name is preceded by the keyword class; • The classes are divided into packages, which group the classes according to their functionality (program start, graphics, mathematical or other operations and so on); • The classes are mutually related through two possible types of relationship: inheritance or composition; • Each class is given a constructor, a particular method that has the same name as the class, which is activated every time a class is created (with the instruction new) and contains all the instructions that must be executed first and only once each time the class is instantiated (created); • The call or activation of a class method by a method of another class can occur every time two classes are related (through inheritance or composition). If the method is public, the call can also be made from a class belonging to another package. Typically, when the program starts with a graphical interface (window), data is usually entered in the graphics package (frame) and this data is then processed by methods belonging to the classes of another package, whose name (mathematics, dictionaries, etc.) will be representative of the type of operations it contains; • At this point we provide the syntax tools that allow you to write any program as a set of classes in relation to each other, containing both data and operations on the data (see yes concept algebraic structure); • We provide information on the important concept of data encapsulation and introduce the use of the keyword private, thanks to which the data of a class are not directly accessible and modifiable by the operations of another class, for security reasons; • Use of get methods are introduced, followed by some setmethods. • We explain the fundamental concept of listener, which allows the program to interact with the operating system. • It should be noted that the language has many pre-defined class packages, which can be used by means of import instructions. • The imported classes that are often used include those that implement the basic data structures of computing: ArrayList, List, Hashmap, Map. In all the dictionaries created in the text examples, the HashMapis implemented, where the clear and constructive implementation of the function concept can be seen. Once the syntactic rules of language and the fundamental principles of object-oriented programminghave been learnt, with reference to examples written correctly in Java, the first step is to start writing programs by modifying or extending the available code, then, in the second step, code is created directly. The first step should not be omitted as, before starting to speak or write in a new language, it is essential to hear it spoken and see it written by those who have already mastered it. Learning syntactic rules is important but not sufficient, not even for a formal language. At the beginning the code writing method may involve a certain degree of automatism, for example a second button is created by observing how the first one was created, a class is instantiated and then a method is recalled, taking an example from a similar operation, and so on. It is in fact a constructive automatism, which helps to fix the essential basic linguistic models in the mind, which will then allow creative code to be generated later on. The correction of projects written by the students must be constant and immediate, in order to allow them to interact in a continuous manner with the code, which should lead to working programs as quickly as possible. When possible, hours of lectures can be reinforced by e-learning activities, thus using learning techniques in blended learning . Type 1 information containers For years we have called them e-books, even if the projects do not fully correspond to what is generally known as ebooks . Students create hundreds of them each year, choosing the multilingual, heterogeneous contents that most interest them . When they create these information containers that are interactive and can always be modified, they come into contact with very important aspects of language, including, not in order of importance: • Interfaces • Inheritance • XML files • String management • Redefinition of the paintComponent () method and use of the device context • Uploading images as wallpapers • Creation of multiple panels • Scroll management • HashMap, list, ArrayList and other Collections with parameterised types • The BufferedImage class and the getRGB () method with practical applications such as geographic maps (selection of a region by colour, etc.) • Use of the coordinates of an image to identify some of its parts (identification of planets and so on) • Links to websites through the URL and URI classes Finally arriving at the creation of a jar executable from any project, which can be run outside of an integrated environment and used on any computer on which the Java run-time has been installed. From a mathematical point of view, important concepts such as those of function and of algebraic structure can be seen applied in practice. As all this may be contained within a single program, we will be able to document it in detail. In any case, bringing together many significant aspects of language in one project helps to understand and remember them more easily.
The ability to adapt a software artifact is essential toward handling evolving stakeholder requirements. Adaptation is also vital in many areas where software is required to adjust to changing environment conditions (e.g., the growing presence of embedded systems). Current techniques for supporting adaptability and evolvability can be categorized as static (happening at compile-time or design-time), or dynamic (adaptation during the actual execution of the system). This paper describes a specialtopics software engineering course that uses Java as a foundation for teaching concepts of static and dynamic adaptation. The course surveys Java-related research in the areas of meta-programming and reflection, aspect-oriented software development, model-driven computing, and adaptive middleware.
We have designed this third edition of Java, Java, Java to be suitable for a typical Introduction to Computer Science (CS1) course or for a slightly more advanced Java as a Second Language course. This edition retains the “objects first” approach to programming and problem solving that was characteristic of the first two editions. Throughout the text we emphasize careful coverage of Java language features, introductory programming concepts, and object-oriented design principles.
2012
As you start programming with Java, it pays to become familiar with the Java keywords as quickly as possible. These keywords have very specific and very helpful meanings within the language. You can't use a keyword for anything other than its stated purpose, but when used for its stated purpose, a keyword is a powerful tool.
Acm Sigcse Bulletin, 1998
The goal of this working group was to collect, evaluate, and foster the development of resources to serve as components of both new and revised traditional courses that emphasize object-oriented software development using Java. These courses could, for example, integrate Internet-based distributed programming, concurrency, database programming, graphics and visualization, human interface design and object-oriented development. They could therefore also be suitable as capstone courses in computer science. The focus of the working group was on tools and techniques, including demonstrations, projects, syllabi, and pedagogical patterns. The working group members are coordinating the development of a Web site (sol.pace.edu/iticse98) devoted to sharing such tools and techniques among educators.

Loading Preview
Sorry, preview is currently unavailable. You can download the paper by clicking the button above.