Download presentation
Presentation is loading. Please wait.
Published byDayna Holt Modified over 9 years ago
1
CSCE 2013L: Lab 1 Overview Java Basics The JVM Anatomy of a Java Program Object-Oriented Programming Overview Example: Payroll.java JDK Tools and Classpath Lab Programs Pay.java Compile and execute a simple Java program SalesTax.java Basic program debugging
2
The Java Virtual Machine Source.java compiles to Source.class as bytecode for the JVM The JVM converts the bytecode into native machine code http://java.sun.com/docs/books/jvms/second_edition/html/VMSpecTOC.doc.html
3
Object-Oriented Programming Overview Object Data: Known as its “attributes” and represents the object's state Procedures: When a procedure is part of an object, it is called a “method”. They represent the interface to use the object. Encapsulation Usually refers to combining procedures and data in this way. Offers data-hiding to the object so that only the object can access its attributes. This lets the object keep itself in a valid state.
4
OOP Overview cont. Access Control The means through which an object controls who can see and manipulate its data and methods. Public: Anyone can access. This is part of the interface. Private: This is part of the internal implementation. Only that object has access. Class This is the prototype or blueprint that allows one to make individual “instances” of an object.
5
OOP final Inheritance Provides a way to specialize classes to add new functionality or change the behavior to something more suitable. The class that is inherited from is the “base” class and is known as a “superclass.” The inheriting classes are “derived” classes and are “subclasses.” Subclasses gain all the attributes and methods of the superclass and can define completely new ones or change the behavior of existing methods.
6
Example: Payroll.java public class Payroll { public static void main(String[] args) { int hours = 40; double grossPay, payRate = 25.0; grossPay = hours * payRate; System.out.println("Your gross pay is $" + grossPay); }
7
JDK Tools javac used to compile Source.java into bytecode outputs Source.class Example: javac Payroll.java output: Payroll.class java starts an instance of the JVM that runs the bytecode on the target platform Example: java Payroll There is no need to specify.class.
8
Classpath Provides a way to tell the Java compiler and JVM where user-defined classes and packages are located. It can be specified for javac or java through the command line option -cp /path/classes or through the environment variable CLASSPATH Windows: set CLASSPATH=C:\location\classes Linux: export CLASSPATH=/path/classes
9
Packages Provides a way to organize classes into functional units and avoid name clashes with other libraries. Declare “package base.sub.etc;” at the top of a file. The corresponding directory/folder structure on disk would be “base/sub/etc” java[c] -cp rootdir [options] where “rootdir” means the directory that contains base and its subfolders (but is not base itself).
10
Pay.java and SalesTax.java Pay.java javac Pay.java java Pay The calculation changes based on number of hours worked. Figure out this point and test above, at, and below it. SalesTax.java Contains syntactic and semantic errors Correct these errors so that it outputs the correct sales tax and final price Document the errors and their lines in the provided worksheet
11
Caveats These programs expect their numeric input in the form of a double. Therefore, make sure to end the integer part with a dot (.) if you desire a whole number. Examples: 40. 40.0 40.5 You will get an exception at runtime (a program failure) if you fail to format the input in the manner expected.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.