1 / 14

An Overview of JAVA

An Overview of JAVA. From basic to slightly less basic. Jonathan-Lee Jones. Overview. Why JAVA? The VERY basics Data Types Functions & Methods. Why JAVA?.

devon
Download Presentation

An Overview of JAVA

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. An Overview of JAVA From basic to slightly less basic Jonathan-Lee Jones

  2. Overview • Why JAVA? • The VERY basics • Data Types • Functions & Methods

  3. Why JAVA? The programs that we are writing are very similar to their counterparts in several other languages, so our choice of language is not crucial. We use Java because it is widely available, embraces a full set of modern abstractions, and has a variety of automatic checks for mistakes in programs, so it is suitable for learning to program. There is no perfect language, and you certainly will be programming in other languages in the future.

  4. The VERY Basics Programming in Java. We break the process of programming in Java into three steps: Create the program by typing it into a text editor and saving it to a file named, say, MyProgram.java. Compile it by typing "javacMyProgram.java" in the terminal window. Run (or execute) it by typing "java MyProgram" in the terminal window. The first step creates the program; the second translates it into a language more suitable for machine execution (and puts the result in a file named MyProgram.class); the third actually runs the program.

  5. The VERY Basics public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World"); } }

  6. The VERY Basics class UseArgument { public static void main(String[] args) { System.out.print("Hello, "); System.out.print(args[0]); System.out.print(", "); System.out.print(args[1]); System.out.print(", "); System.out.print(args[2]); System.out.println("!"); } }

  7. The VERY Basics class UseArgument { public static void main(String[] args) { if(args.length>0){ System.out.print("Hello, "); System.out.print(args[0]); System.out.print(", "); System.out.print(args[1]); System.out.print(", "); System.out.print(args[2]); System.out.println("!"); System.out.println } Else System.out.println(“Error!”); } }

  8. Data Types There are many built in types in JAVA. These built in types can be primitive types, or object types. There are more options available as to what the programmer can do with the more complicated objects than the simple built in primitive types.

  9. Functions & Methods public class Max2 { public static int max(final int a, final int b) { if (a>b) return a; elsereturn b; } public static void main(final String[] args) { final int a = Integer.parseInt(args[0]); final int b = Integer.parseInt(args[1]); System.out.println("The maximum of " + a + " and " + b + " is " + max(a,b) + "."); } } It is often easier to write functions to do a certain task. For example, if you know you are going to require a certain type of calculation repeatedly. Functions are usually of the type static, and methods dynamic, but apart from this they are very similar.

  10. Functions & Methods public class Max4 { public static int max(final int a, final int b) { if (a>b) return a; elsereturn b; } public static int max(final int a, final int b, final int c) { return max(max(a,b),c); } public static int max(final int a, final int b, final int c, final int d) { return max(max(a,b,c),d); } public static void main(final String[] args) { final int a = Integer.parseInt(args[0]); final int b = Integer.parseInt(args[1]); final int c = Integer.parseInt(args[2]); final int d = Integer.parseInt(args[3]); StdOut.printf("The maximum of %d, %d, %d, %d is %d.\n", a,b,c,d,max(a,b,c,d)); } } Functions can also be overloaded. This means that you can have a different function (in this case max) with the same name, but taking different inputs. JAVA identifies the correct one to use at compile time.

  11. Functions & Methods The term methods usually refers to dynamically called functions. These are not determined at compile time, but are called implicitly on an object. For example this.minVal() would run the minVal method on this object. Methods are often used when the user creates objects of their own. A good example of this is a block object

  12. Functions & Methods class Block{ private double height; private double width; private double length; public Block(double height, double width, double length) { this.height=height; this.width=width; this.length=length; } public double getTopArea() //legth x width { return length*width;} public double getFrontArea() //length x height {return length*height;} public double getSideArea() //width x height {return width*height;} public double getVolume() //area x height {return length*height*width;} }

  13. Functions & Methods class BlockTest{ public static void main(String[] args){ if(args.length<3) { System.err.println(“Not enough arguments”); return; } else { double L = Double.parseDouble(args[0]); double W = Double.parseDouble(args[0]); double H = Double.parseDouble(args[0]); Block testB = new Block(L,W,H); System.out.println(“Volume = “ + testB.getVolume()); } } }

  14. Thank You

More Related