0% found this document useful (0 votes)
59 views

Ch. 1 Getting Started W - Java

This document provides an overview of key concepts in computer programming and the Java programming language. It defines terms like computer program, hardware, programming language, machine language, assembly language, assembler, bit, byte, low-level languages, high-level languages, source code, interpreted languages, compiled languages, Java Virtual Machine, procedural programming, object-oriented programming, classes, objects, packages, and methods. It also describes the main() method, identifiers, reserved words, comments, and how to use the System class and print method to output text. Finally, it introduces the javax.swing package for graphical user interfaces and how to compile and run Java programs using the javac and java commands.

Uploaded by

thateggo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views

Ch. 1 Getting Started W - Java

This document provides an overview of key concepts in computer programming and the Java programming language. It defines terms like computer program, hardware, programming language, machine language, assembly language, assembler, bit, byte, low-level languages, high-level languages, source code, interpreted languages, compiled languages, Java Virtual Machine, procedural programming, object-oriented programming, classes, objects, packages, and methods. It also describes the main() method, identifiers, reserved words, comments, and how to use the System class and print method to output text. Finally, it introduces the javax.swing package for graphical user interfaces and how to compile and run Java programs using the javac and java commands.

Uploaded by

thateggo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Chapter 1 - Introduction

•Computer Program/Software – a set of instructions and data used to operate a


computer to produce a specific result.
•Hardware - the physical aspect of a computer (CPU, motherboard, memory, etc.)
•Programming – writing a program
•Programming Language – set of instructions used to construct a program
•Machine Language Programs – referred to as executable programs or executables.
Consist of ones & zeros 110000000000100000… The instruction part is known as
“opcode” and is usually the first part of each binary number
•Assembly Language – substitution of “opcode” with meaningful word-like symbols
such as ADD, SUB, MUL.
Example: LOAD first
ADD second
MUL factor
STORE answer
•Assembler – translator program which converts assembly language into
machine language programs
Chapter 1 - Introduction
•Bit - 1s and 0s, the smallest piece of information a computer can use
•Byte - 8 bits.
•Low Level Languages - Machine level and Assembly languages are
considered low level. Both of these are specifically tied to one type of
Computer, e.g. a program written on Intel won't work on an AMD.
•High Level Languages – Java, C/C++, Visual Basic. These resemble natural
languages, and can be run on varieties of computers
•Source Programs/Source Code – Programs written in a computer
language (high or low)
•Interpreted Language – each statement in a high-level source program is
translated(by an interpreter) individually and executed immediately
upon translation. Java is an interpreted language.
•Compiled Language – all the statements in a high-level source program
are translated (by a compiler) as complete unit before any one statement
is executed
•Java Virtual Machine (JVM) – the interpreter for the Java language. It
interprets a .class Java bytecode file.
Object Oriented vs Procedural
•Procedural Languages- break programs down into a collection of
procedures or functions. Each function is a sequence of steps to
perform a task. For example a "car" program will have procedures to
stop, accelerate, turn left, turn right, etc.

•Object Oriented Programming (OOP) Language - the procedures


above are grouped into a new "entity" known as a class. The "car" class
will serve as a template from which many "car" objects can be created.
For example, a Sports car object or a Sedan car object are both
"instances" (types of cars) which incorporate the same functions of the
original "car" template/class, with some variations. For example some
cars have faster acceleration and shorter stop distance. In other words,
OOP language allows you to group Properties (variables or data) and
Actions (methods or functions) and reuse them via instances, where
instances can have different properties. C language was originally a
procedural language. C++ was the OOP evolution of C, where the
procedures of C could be grouped into Objects. Java was originally an
OOP language.
Chapter 1.2- Objects & Classes
•Objects- data entry areas, check boxes, command buttons,
mathematical object types. Real world object can be modeled using
computer science objects, for example a dog object or table object.

•Class– is the “plan/recipe” required to create an object which will be


used by a program. A class is a template or blueprint for objects.

•Package – consists of one or more classes that are stored in the same
directory

•2 Main Sections of Java Class (from which Object is made):


•Data declaration section – lists the type of data needed and a name for each
data
•Methods section – combines the data components to produce a desired result
Chapter 1.2 - A Sample Java Class
public class ShowFirstMessage  Class Header Line
{  Start of Class Body

//class data declaration section Start of Data/Variable Section


private String message;

//class method definition section Start of Method Section


ShowFirstMessage()
{
message = “I need a cup of Java.”;
}

public void displayMessage()


{
System.out.println(message);
}

}  End of Class Body


Chapter 1.3 - The main() method
public static void main (String[] args)  Method Header Line
{
Method Body
}

public – visibility modifier


static – scope designation (how it’s created & stored in computer memory)
void – return type
main – method name
String[] args – argument list
(String – a number of characters enclosed in a quotation marks “Hello World”)

// comment are lines that start with two forward slashes


Comments are not programming instructions(statements), but explanations written for other
people to better understand your programs.

Every single executable Java program must have a main() class.


The main() is where the Java program Starts.
Chapter 1.3- Identifiers, Reserved Words,
Comments

•Identifiers – words that you can/will use to name your classes as well as
other elements(methods, variables) of the Java language. The following
rules apply:
•First character cannot be a digit: 5dollars, 10days
•Only: letters, digits, underscores, $ may follow the initial character
•No blank space: Month One,
•Cannot be a reserved word (page 26)

•Reserved Words – predefined words used by Java for special purposes.


Examples: double, void, if, for, while, switch, public, etc.

•Comment – anything that follows the two forward slash symbols “//”

//this is a comment
Chapter 1.4 - System Class, and print() method
•System– a class that provides a number of methods. Among those methods we
have methods to send data to the screen (output) and methods to accept data
from the user via keyboard (input)

•To print our output, such as “Hello World” , on the computer screen, we use:
System.out.print(“Hello World”);

out – is the name of the “standard output stream” object created from the
“System” class.

•Arguments/parameters - the items that are passed to a method through the


parenthesis, in this case the string “Hello World”
System.out.print(“Hello \n World”);

•\n – these two characters together are called a “newline escape sequence”. It
will cause “Hello” and “Word” to be printed on two separate lines on your screen.
•println() is like print() with an \n at the end.
Chapter 1.5 – javax.swing Package

•Java Package – directory which contains one or more individual classes

•swing Package – provides means of specifying fully functional graphical user


interface (GUI) with typical window components such as: check boxes, data entry
fields, command buttons, and dialogs.

•Dialog – a box that presents the user with output and requires some input. See
page 36-37 for types of dialog boxes and their messages
Chapter 1.5 – javax.swing Package
import javax.swing.*;
public class DisplayADialog
{
public static void main (String[] args)
{
JOptionPane.showMessageDialog(null, “Hello World”,
“Sample”, JOptionPane.WARNING_MESSAGE);

System.exit(0);
}
}
How to get "Java JDK "

• The JDK consists of a set of programs for developing & testing Java programs

• https://www.oracle.com/java/technologies/javase-downloads.html

• Click on "Download" above "Java JDK"

• Accept License Agreement, download from the appropriate link. For windows
chose Windows x64 installer. For Mac choose macOS Installer.

• Open the downloaded file and follow the "typical" installation instructions.

• For more information, please see the videos posted in Week 1 or Week 2
Using “javac” and “java”
• The commands "javac" and "java" are not system commands, but they are Java
commands(programs) located in the Java JDK bin directory. You need to set up the PATH
variable for Windows to know where to find javac and java (see the videos I have).

• Unless you set up PATH, if you are out of the C:\Program Files\Java\jdk\bin directory, the
computer won't know what the(javac, java) commands mean (or where the programs are).

• Suppose I want to run the program HelloWorldApp.java, which by the way is just a text
file containing the following java code:
class HelloWorldApp {
public static void main(String[] args) {
System.out.println("Hello World!"); // Display the string.
}
}
•HelloWorld.java is located in C:\new. First I need to navigate to that folder with the
command prompt (StartRun”command”)…see next page for DOS commands
•If I just type javac HelloWorldApp.java in the command prompt instead, it will give me the
following error message 'JAVAC' is not recognized as an internal or external
command,operable program or batch file.
•I must type C:\Progra~1\Java\jdk\bin\javac HelloWorldApp.java for no error. Upon
success, the HelloWorldApp.class is created.
•Now if I type C:\Progra~1\Java\jdk\bin\java HelloWorldApp, the command prompt will
print “Hello World” on the screen. See next page to visualize all this.
What I just described as seen/done in the command prompt:
C:\DOCUME~1\AGADOR~1>cd..

C:\DOCUME~1>cd..

C:\>cd new

C:\NEW>dir
Volume in drive C has no label.
Volume Serial Number is 38F2-2C3E

Directory of C:\NEW

12/03/2011 02:17 AM <DIR> .


12/03/2011 02:17 AM <DIR> ..
12/03/2011 02:14 AM 1,863 HelloWorldApp.java
1 File(s) 1,863 bytes
2 Dir(s) 44,508,884,992 bytes free

C:\NEW>javac HelloWorldApp.java
'JAVAC' is not recognized as an internal or external command,
operable program or batch file.

C:\NEW>C:\Progra~1\Java\jdk\bin\javac HelloWorldApp.java

C:\NEW>C:\Progra~1\Java\jdk\bin\java HelloWorldApp
Hello World!

C:\NEW> class HelloWorldApp {


public static void main(String[] args) {
System.out.println("Hello World!");
}
}
Copying Program Output
•Once you get the black output
screen stopped, you need to
Mark and Copy the output.

•Up in the top left of the black


output screen, there is a little
box.

•Go to that small box and click,


then, down to Edit, and then click
on Mark.

•Drag over the output (marking


it), and then go back to Edit,
again, and click on Copy, or just
hit “Enter”

Then hit “Enter” to copy. Paste that at the end of the program you are turning in.

You might also like