java4python DePaul University

Java4python Depaul University-PDF Download

  • Date:24 Jun 2020
  • Views:18
  • Downloads:0
  • Pages:50
  • Size:246.85 KB

Share Pdf : Java4python Depaul University

Download and Preview : Java4python Depaul University

Report CopyRight/DMCA Form For : Java4python Depaul University


1 Preface 3,1 1 Preface to the third edition 3,1 2 Preface to the second edition 3. 2 Getting Started 5,2 1 Why Java Dynamic versus Static Languages 5. 2 2 Hello World 6,2 3 Running from the command line 6. 2 4 The Anatomy of HelloWorld 7,3 Basics 11,3 1 Base Types 11. 3 2 Strings 11,3 3 Conversions 12,3 4 Example Fahrenheit to Celsius 12.
3 5 List 16,3 6 Arrays 19,3 7 Conditionals 19,3 8 Loops and Iteration 21. 3 9 Common Mistakes 23,3 10 Naming Conventions 24,3 11 Java Documentation Online 24. 4 Lists and Maps 25,4 1 Numeric 25,4 2 String 28,4 3 List 29. 4 4 Arrays 32,4 5 Dictionary 33,5 Classes 35,5 1 Defining Classes in Java 35. 5 2 Writing a constructor 37,5 3 Methods or Member Functions 38.
5 4 Inheritance 40,5 5 Interfaces 42,5 6 Static member variables 44. 5 7 Static Methods 44,5 8 Full Implementation of the Fraction Class 45. java4python Release 3 0,There is a search and genindex. A PDF version and source code are available,CONTENTS 1. java4python Release 3 0,2 CONTENTS,1 1 Preface to the third edition.
Welcome to Java for Python Programmers This is a quick introduction to Java for programmers that already know. another language preferably Python, We will begin by looking at a very simple Java program just to see what the language looks like and how we get a. program to run Next we will look at the basic syntax of Java and relate it to python We will then spend some time. looking at inductively defined functions and array Finally we will look at how to define our own classes in Java. This edition of the text is adapted to the data structures classes taught at DePaul As our primary text we use Algo. rithms 4e The main changes are to the section on classes I ve also added a new section on inductive programming. over arrays using iteration and recursion,James Riely jriely cs depaul edu December 2013. This work is licensed under a Creative Commons Attribution 3 0 United States. License See http creativecommons org,1 2 Preface to the second edition. Welcome to Java for Python Programmers This short ebook is an ongoing project to help Computer Science students. who have had one or two semesters of Python learn the Java programming language If you are not a part of that. audience you may still find this a useful way to learn about Java This book is written using the build on what you. know philosophy In order to help you learn Java I will start with a Python example and then implement the example. in Java Along the way we will examine the strengths weaknesses and differences between those two languages. This book does not attempt to replace the many good Java reference books that are available in fact I use this in my. course along with Horstman s Core Java volumes Please feel free to use this book for yourself or if it fits a class you. are teaching you are welcome to use this as a resource for your own class. I have published this article using a Creative Commons license to encourage you to use it change it and modify it for. your own purposes I would appreciate knowing what you think if you do use this book and I would love to see any. modifications or additions you make,Brad Miller bmiller luther edu January 2008. This work is licensed under a Creative Commons Attribution 3 0 United States. License See http creativecommons org,java4python Release 3 0.
1 2 1 Shameless Plug, At Luther college we use Python for CS1 and CS2 When we decided to make the switch to introducing CS with Python. we wanted to provide our students with two semesters of Python The reason is that after one semester students are. just getting comfortable with the language and it does not make sense to push them into a brand new language just as. they are getting some comfort The second reason is that Python really is a great language for teaching data structures. The language makes the algorithms evident by clearing away all of the syntactic clutter So we David Ranum and I. wrote a CS2 book called Problem Solving with Algorithms and Data Structures using Python After we finished the. CS2 book we decided to write our own CS1 book as well This book will be available for Fall 2008 classes from Jones. and Bartlett publishers The tentative title is Python in Context Please feel free to contact me for more information. 4 Chapter 1 Preface,GETTING STARTED,2 1 Why Java Dynamic versus Static Languages. Python is a nice language for beginning programming for several reasons. The syntax is sparse and clear, The underlying model is very simple Everything is an object. You can write powerful and interesting programs without a lot of work. Python is representative of one kind of language called a dynamic language Dynamic languages can be interpreted. directly which means that the actual text of the program the source code is used while the program is running. In contrast a static language is executed in two phases first the program is translated from source code to binary. code and then the binary code is interpreted Although the terms dynamic and static language are widely used the. distinction is a fuzzy one Most execution engines do both translation and interpretation. Static refers to what the translater does The translater is called a compiler. Dynamic refers to what the interpreter does, Dynamic languages do more in the interpreter By doing more in the interpreter dynamic languages have more. flexibility By doing more in the compiler static languages gain their own advantages. Compiled languages are fast They gain speed in at least two ways. First by distinguishing objects from builtin types aka base types In order to do basic arithmetic a dynamic. language may need to convert from an object to base type and then back a static language avoids the conversions. Second by performing name lookups in advance Dynamic languages use the name of a method or a de. rived selector to look up the method during interpretation Compilation makes this much more efficient often. resolving the actual method in advance, Compiled languages are excellent for writing large programs that are maintained over time Python and other.
scripting languages require that the programmer keep track of a lot of information For example if you set vari. able x to reference a turtle and forget later that x is a turtle but try to invoke a string method on it you will. get an error Compiled languages track this information using types for variables This means that program. mers need to keep track of less information informally reducing the chance of miscommunication between to. programmers, In addition compiled languages support a notion of hiding Java allows you to declare some data to be private. meaning that it is only accessible by certain method and not others Python uses naming conventions for the. same thing variables starting with are meant to be ignored by most methods but there is nothing that. enforces this, Python is representative of a whole class of languages sometimes referred to as scripting languages Other languages. in the same category as Python are Ruby and Perl Java is representative of what I will call industrial strength. java4python Release 3 0, languages which include C C and Scala Industrial strength languages are good for projects with several people. working on the project where being formal and careful about what you do may impact lots of other people. Java is the most widely taught programming language. Java is one of the most widely used programming languages. Java is industrial strength used for large systems by large groups of people. Java is simpler than C or Objective C, Although Java is an enormous language the core language is very small The features that we will use are common to. every static language If you want to program in C or Objective C you should learn Java first. 2 2 Hello World, A time honored tradition in Computer Science is to write a program called hello world The hello world program.
is simple and easy There are no logic errors to make so getting it to run relies only on understanding the syntax To. be clear lets look a a complicated version of hello world for Python. print Hello World, Remember that we can define this program right at the Python command line and then run it. Hello World,Now lets look at the same program written in Java. 1 public class Hello,2 public static void main String args. 3 System out println Hello World, What we see is that at the core there are a few similarities such as a main and the string Hello World However there. is a lot more stuff around the edges that make it harder to see the core of the program Do not worry An important. skill for a computer scientist is to learn what to ignore and what to look at carefully You will soon find that there are. some elements of Java that will fade into the background as you become used to seeing them One thing that will help. you is to learn a little bit about Java Naming Conventions. 2 3 Running from the command line, The first question you probably have about this little program is How do I run it Running a Java program is not as.
simple as running a Python program The first thing you need to do with a Java program is compile it First we must. type the hello world program into a file and save that file using the name Hello java The file name must be the. same as the public class you define in the file Once we have saved the file we compile it from the command line as. 1 ls l Hello, 2 rw r r 1 bmiller bmiller 117 Jul 19 17 46 Hello java. 3 javac Hello java,4 ls l Hello,6 Chapter 2 Getting Started. java4python Release 3 0, 5 rw r r 1 bmiller bmiller 391 Jul 19 17 47 Hello class. 6 rw r r 1 bmiller bmiller 117 Jul 19 17 46 Hello java. The command javac compiles our java source code into compiled byte code and saves it in a file called. Hello class Hello class is a binary file so you won t learn much if you try to examine the class file with an. editor Hopefully you didn t make any mistakes but if you did you may want to consult the Common Mistakes section. for helpful hints on compiler errors, Now that we have compiled our java source code we can run the compiled code using the java command. java Hello,Hello World, Now you may be wondering what good is that extra step What does compiling do for us There are a couple of.
important benefits we get from compiling,Early detection of errors. Faster Program Execution, The job of the compiler is to turn your java code into language that the Java Virtual Machine JVM can understand We. call the code that the JVM understands byte code The JVM interprets the byte code much like the Python interpreter. interprets your Python However since byte code is much closer to the native language of the computer it can run. When the compiler does the translation it can find many different kinds of errors For example if you make a typo the. compiler will find the typo and point it out to you before you ever run the program We will look at some examples of. compiler errors shortly Chances are you will create some on your own very soon too. 2 4 The Anatomy of HelloWorld, Now that we have run our hello world program lets go back and look at it carefully to see what we can learn about the. Java language,1 public class Hello,2 public static void main String args. 3 System out println Hello World, This simple example illustrates a few very important rules.
1 Every Java program must define a class all code is inside a class. 2 Everything in Java must have a type, 3 Every Java program must have a function called public static void main String args. Lets take the hello world example a line at a time to see how these rules are applied On line 1 we see that we are. declaring a class called Hello As rule 1 says all Java code resides inside a class Unlike Python where a program can. simply be a bunch of statements in a file Java programs must be inside a class So we define a class Hello is not. a very useful class it has no instance variables and only one method You will also notice the curly brace In Java. blocks of code are identified by pairs of curly braces The block starts with a and ends with a You will notice that. I indented my code that followed the left brace but in Java this is only done by convention it is not enforced. On the next line we start our method definition The name of this method is. 2 4 The Anatomy of HelloWorld 7,java4python Release 3 0. public static void main String args, Everything on this line is significant and helps in the identification of this method For example the following lines. look similar but are in fact treated by Java as completely different methods. public void main String args,public static void main String args. public static void main,void main String args, Just digging in to this one line will take us deep into the world of Java so we are going to start digging but we are not.
going to dig too deeply right away Much of what could be revealed by this one line is better understood through other. examples so be patient, The first word public indicates to the Java compiler that this is a method that anyone can call We will see. that Java enforces several levels of security on the methods we write including public protected and private. The next word static tells Java that this is a method that is part of the class but is not a method for any one. instance of the class The kind of methods we typically w. who have had one or two semesters of Python learn the Java programming language If you are not a part of that audience you may still nd this a useful way to learn about Java This book is written using the build on what you know philosophy In order to help you learn Java I will start with a Python example and then implement the example in

Related Books