DOC PREVIEW
TRINITY CSCI 1321 - Java Basics

This preview shows page 1-2-20-21 out of 21 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 21 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 21 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 21 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 21 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 21 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Java Basics(Assuming Alice Background)byDr. Mark C. LewisforIntroduction to Programming LogicIntroductionThis document is intended to provide a quick overview to the Java programming language as well as some examples of how to use it. The Java language is actually a simple programming language, but like all programming languages, it is very picky about the rules being followed. This is in contrast to natural languages which are inevitably extremely complex, but don't have such problems if rules are occasionally broken. Of course, what matters most in this comparison is the audience the language is used to communicate with. Natural languages are used to communicate with other humans and generally other humans are forgiving of your mistakes and can get the needed information out of what is said, even if it isn't said quite right. Programming languages are used to give instructions to a computer to tell it what to do. This process needs to be formal and rigorous because there can't be ambiguity. Also, the computer is a simpler device than the human so you have to really follow the rules closely for it to understand what you are telling it to do.Basic Rules/TipsThere are some basic rules that can be laid out for the construction of a Java program. Some of them just make sense, like the fact that you need to match up parentheses. Others are more specific to Java, like the fact that all of your code needs to be placed inside of classes. We'll start by just giving a list of some of these rules that you will want to follow and then give explanations for some of them.• Java code is constructed by writing classes. Each file needs to have one public class with the same name as the file.• Classes can contain the properties of objects of that class and methods that give the behaviors of those objects.• Every open parentheses, brace, or bracket needs to have a matching close.• You can't put methods inside of methods so make sure you close off the curly braces for one method before starting another.• Variable declarations have the format of a type followed by the name of the variable. Java allows you to declare variables anywhere inside of a method.• Import statements are the only things you will write outside of classes and if you use Eclipse you can use Ctrl-Shift-O and Ctrl-Shift-M to do imports and it won't misplace them.• We call methods on objects using the dot notation. The syntax is object.method(args). Even if there are no arguments you need the parentheses, but you can leave them blank.When you start writing a statement in Java there generally aren't many options that you have. There are only a few things that are valid for writing a statement or an expression in Java. In most of your statements you will be either asking an object to do something or you will be asking an object for some information that you can evaluate or store in a variable. That means that you will start with the name of a variable that is in scope at that line. These are either parameters to the method, local variables, or properties. Generally you don't have many of these so there really aren't many options for what you can do in any given expression. Use that to help focus and simplify your thinking.Non-Object Oriented ProgrammingJava is a primarily object oriented programming language, just like Alice was. What does that mean? Well, an object is a construct in a programming language that combines data and the functionality to manipulate that data. That is to say that an object is something that has properties and methods that operate on the properties. Despite the fact that Java is intended to be object oriented, not all Java programs have to be object oriented. The static keyword tells us that a method or property is associated with a class instead of with individual objects. Programs that only use static methods aren't really object oriented because they don't actually have to create new objects. While such programs don't use all of the benefits of the Java language, they can be simple ways of illustrating the logical structure we use to solve simple problems. For this reason, most of the early programs in the course only use static methods and we'll start with that in this document as well.Let's run through a simple example of a program that we can write using several Java constructs in a non-object oriented way. This program reads numbers from the user and keeps reading until they enter zero. It will print out two averages. One is the average of the positive numbers and the other is the average of the negative numbers. Notice that all of this code can goinside of a main. Basically, the description of the problem didn't really involve any “objects” so we don't need to be writing multiple classes. The only objects we will use are from the Java libraries for doing input and output. I'm going to leave out all the import statements in all code examples to save space. Eclipse can add them for you easily enough.public class Averages { public static void main(String[] args) { double positiveSum=0,numPositive=0; double negativeSum=0,numNegative=0; Scanner sc=new Scanner(System.in); System.out.println("Enter numbers. Type 0 to quit."); double num=sc.nextDouble(); while(num!=0) { if(num>0) { positiveSum+=num; numPositive++; } else { negativeSum+=num; numNegative++; } num=sc.nextDouble(); } if(numPositive>0) { System.out.println("The positives had an average of "+ positiveSum/numPositive); } else { System.out.println("No positive numbers were entered."); } if(numNegative>0) { System.out.println("The negatives had an average of "+ negativeSum/numNegative); } else { System.out.println("No negative numbers were entered."); } }}Let's walk through the logic involved in making this code. The program always starts in main and this is a fairly simple program so we just put everything inside of main. We know that we need to read in numbers so we make a Scanner object. We also know that to find an average of a bunch of numbers we have to have the sum of the numbers and the how many numbers there were. For this reason there are four doubles declared to keep


View Full Document

TRINITY CSCI 1321 - Java Basics

Documents in this Course
Recursion

Recursion

11 pages

Iterators

Iterators

10 pages

Actors

Actors

9 pages

Recursion

Recursion

15 pages

Recursion

Recursion

10 pages

Threads

Threads

7 pages

Trees

Trees

11 pages

Load more
Download Java Basics
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Java Basics and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Java Basics 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?