Week 1 - Introduction to Java Lecture Notes PDF

Summary

These lecture notes cover an introduction to Java, including course information, the module leader and co-teachers, course layout and syllabus, and the schedule. The document also outlines the workload, assessment details, and office hours for the course.

Full Transcript

Week 1: Introduction to Java Yushan Pan Department of Computing Course introduction What to expect from this course About me Course layout + Syllabus Tips to succeed in this course Quiz Individual assignment Coursework rules Exam About me PhD in Informatics, Design Group (former...

Week 1: Introduction to Java Yushan Pan Department of Computing Course introduction What to expect from this course About me Course layout + Syllabus Tips to succeed in this course Quiz Individual assignment Coursework rules Exam About me PhD in Informatics, Design Group (former system development group) at the University of Oslo, where Object- Oriented Programming and Analysis were born. Simula language, 2001 Turing Award (Kristen Nygaard and Ole-Johan Dahl, University of Oslo) Research interests: Computer-supported Cooperative Work, Human-Computer(Robot) Interaction and Participatory Design. Six years of industry experience with PTC Inc., USA, Kongsberg Maritime, Norway and Rolls Royce, UK Associate Chair for ACM CHI and CSCW conferences and ACM SIGCHI CARES committee Your module leader and co-teachers Dr. Yushan Pan, Module leader (Week 1 - Week 3) Dr. Erick Puranto (Week 4 - Week 5) Dr. Haiyang Zhang (Week 6 - Week 10)* Dr. Chengtao Ji (Week 11 - Week 13) * Week 7 is reading week. Week 8 is Coursework 2 test. No new lectures in these two weeks. Course layout and syllabus Course layout: Text-based instructional content, which sometimes includes exercises and examples for you to work on your own At the beginning of each module, you will find module resources (on the learning mall), including the slides corresponding to the lectures, data sets, and coding files you can download while working on all coding exercises Syllabus: No mandatory textbook, but you can see optional choices on handbook Personal recommendation – Bruce Eckel, On Java 8 Joshua Bloch, Effective Java Minimal or no prior computing experience Going back to school What the Advancing a career in information course is technology for? Learn the basics of programming Great refresher What are included Introduction to Java and Object- oriented programming Overview of Java syntax Custom Java classes and methods Test-driven development and unit testing Generate code documentation in IntelliJ (or Whatever you prefered) Arrays and array lists Overloading methods Course Schedule Detailed schedule – Handbook on LMO Lectures: Timetables are on eBridge Lab sessions: Timetables are on eBridge Additional support classes are available by appointment. You can request this on LMO. Workload 2 hours lecture per week 2 hours lab sessions per week Coursework 1 - Graded programming assignments throughout the semester (individual) Coursework 2(an on-site test) and 3 (individual project - within 5 working days) Exam (individual) Note: Your final grade of CPT 105 = Workload - Resit Exam It is IMPOSSIBLE to resit Coursework. If you failed the module, you must take resit Exam (100%). If you pass the module but fail either the coursework or final exam, you cannot carry the marks from the passed component into the resit exam. For a MC exam, the resit exam will account for 100% of your final mark for the course, and your coursework will not be considered. Lectures Onsite Lecture Delivery One lecture a week PTT will be provided Read the PowerPoint to make sure you understand the language! After the lecture Review the ppt. Do you understand it all? Discuss it with your classmates Ask questions on the Learning Mall Forums (All teachers will answer!) During lecture slots, we will monitor forums. Labs One 2 hour lab per week: A task sheet is on Learning Mall Programming practice Related to the lecture material Work through it, talk to your classmates, and ask the TAs questions! Exercise sheets You need to practice MORE than 2 hours Two hours a week is not enough to become a good coder Complete the lab sheet in your own time Exercise sheets are extra practices (not mandatory) Will help you become a better coder Assessment 3 Coursework Assignments and an Exam CW1 – Continuous assessment. 15% Weekly programming challenges Each one contributes to your final grade Will be issued during each Assessment 3 Coursework Assignments and an Exam CW 2 – Java test 15% Will count for 15% of your total grade An online test on Week 7 Assessment 3 Coursework Assignments and an Exam CW3 – Java project 30% A programming task to do in your own time Challenging programming task, issued around week 10 5 days to complete the task Will count for 30% of your total grade Exam - Written Exam 40% Signal choice questions Written questions My office hour and room Tuesday: 16:00 – 17:00 pm Wednesday:16-17 Room: SD435 Please send me an email at least one working day before our meeting. Co-teachers’ office hours and rooms Haiyang Zhang, Tuesday 13-14, Wednesday 13-14, Email: [email protected] Chengtao Ji, Tuesday 14-15, Wednesday 14-15, Email: [email protected] Erick Purwanto, Tuesday 15-16, Wednesday 15- 16, Email: [email protected] How to be successful in this course? Only two hours lecture per week is insufficient. More practices are needed. Practice your coding skills via assignments on learning mall every week. Attend all lab sessions. Askcheat. Don’t your TAs for help. Don’t shy. Coding is a hand-on Coursework skillyou 1 will help thattocan boost your understand careers. most basic principles Cheating of serious is a very Java programming. issue. However, you must Fromneed this to read year, wea will book in order have to recap your our departmental learnings fromas exam officer theco-teacher classes. for this course. Most predominant Java concepts come from the SmallTalk language, which insists that the object is Introduction the fundamental unit of programming. The principle of SmallTalk comes from the Simula language, that is everything must be an object. Some folks have declared that objects are a Some voices complete failure and should be discarded. about object Some background of the object might not know Norwegian trade unions projects Planning, control, and data processing All are evaluated in light of the The objectives of organised labour. Computer artefacts and human operators share the same eligibility in background computer systems. of object Heavily influence the Scandinavian School in System Development, which is closely linked to the field of Participatory Design and Computer- Supported Cooperative Work Programming is about managing complexity: What the the complexity of the problem laid upon the programming complexity of the machine. In contrast, the complexity can cause many is used for? programming projects to fail. My goal in this course Everyone Every language has design errors. makes Understanding of language and library design errors is essential because of their impact on mistakes programmer productivity. A tip to android programmers Android programming with java is tied to Java 5. If you want to program in Java for Android devices, you're better off with Thinking in Java, 4th edition. Kotlin is a newer and much better choice – and an official language- for Android programming. JDK HTML Documentation You can download the Java development kit (JDK) from Oracle. This course is not about the JDK but offers you a much faster way to find the class description with you browser. However, we cover Java 11 or later versions. If you have older ones, please update it. https://www.oracle.com/java/technologi es/javase/jdk11-archive-downloads.html https://www.oracle.com/java/technologi es/downloads/ Week 1 Data. Variables. A simple Java program. This week Java Virtual Machine Bits and Bytes High and Low Level Languages Data Types in Java Programming In IntelliJ What is a computer? Generally, a computer: Takes input data of some sort. Processes the data. Produces output data (information) of some sort. Stores data. Is controlled by one or more programs. What is a program? A computer program is a set of instructions to tell the computer exactly whatintoa do. It is written language like Java or C#, a human read/write-able language. A compiler then converts this into executable (.exe) bytecode, which a computer can read/write. Different operating systems (OS) require different executables produced by different languages: Compiling Different Operating systems have different ways of running instructions 32 and 64 bit processors have different way of addressing data This means that a number of different versions of the same program may be required Traditional benefit of console gaming – known hardware! Java programs can (theoretically) run on all computers So how can Java apps run on any OS? Desktop/server Java apps can run on Windows, Mac OS, Unix, Linux etc. How? Each of these OSs has their own JVM, or Java Virtual Machine – a virtual PC running inside a real one. JVMs translate java instructions into instructions in the OS’s own language. JVMs can run Java bytecode from programmes written in Java. The Java Virtual Machine (JVM) This is what we write in IntelliJ. It is a.java Java source code Eg: HelloWorld.java text file Javac IntelliJ does this for you. Others do the same. compiler This is created by Eg: HelloWorld.class the compiler. It is a Java bytecode.class file JVM for JVM for Unix Windows Unix OS Windows OS 34 How does Java work? A translator translates an entire text or book before publication – at compile time..java file. You write this. The compiler is a translator. It translates from human-readable language to Bytecode (1s and 0s). All java bytecode will be read the javac compiler same way by the jvm..class file. Bytecode which An interpreter interprets/translates a conversation as runs on the JVM. it is happening – at run time (runtime). The JVM is an interpreter. It Java Virtual interprets standard Java bytecode Machine (JVM) into the language of the computer That’s why in Java it is running on (Windows, Mac OS, we talk about Linux, iOS, Android etc). A different JVM is needed for every OS. compile time Underlying OS (Operating System) errors and runtime errors. High and Low Level Languages Computers use binary for their instructions We do not program in binary This would be a nightmare to write and proofread So many errors Programming languages developed to help They can be low level, or high level Low Level to High Level Languages Binary: 1000110100111010100100100111 Assembly: TAD I Z STPTR / Get next character SNA / Skip if non-zero (not end of string) HLT / Else halt on zero (end of string) TLS / Output the character in the AC to the teleprinter C C#, Java System.out.println(“Hello World!”); Lowest Level - Binary Binary: 1000110100111010100100100111 Assembly: Binary, what the Computer understands. A TAD I Z STPTR stream / Get next of 1s and 0s. Not feasible character to program in. SNA / Skip if non-zero (not end of string) HLT / Else halt on zero (end of string) TLS / Output the character in the AC to the teleprinter C C#, Java System.out.println(“Hello World!”); High and Low Level Languages Low Level Languages SUB AX,AX MOV ES,AX Very simple commands SUB BH,BH Subtract, add, copy, delete MOV A command and then values BL,INT_NUMBER SHL BX,1 Interacts directly with memory and registers Assembly language A low level language Easier to program in than binary! Still not clear to read Low Level – Assembly Language Binary: 1000110100111010100100100111 Assembly: TAD I Z STPTR / Get next character SNA / Skip if non-zero (not end of string) HLT / Else halt on zero (end of string) TLS / Output the character in the AC to the teleprinter C Assembly is a low level language. Very simple C#, Java SUB AX,AX commands, which can be easily translated to MOV ES,AX binary commands. SUB BH,BH MOV BL,INT_NUMBER Difficult to read and follow! SHL BX,1 CPU instructions use this format High and Low Level Languages High Level Language System.out.println(“Hello World!”); English like syntax Easier for humans to read and program in Many different languages Java, C, C#, C++ etc. One high level instruction may be translated into several low level language instructions High Level Languages C, C#, Python, Java etc. Binary: 1000110100111010100100100111 Different types of High Level Language Assembly: English-like syntax TAD I Z STPTR / Get next character SNA Readable by humans / Skip if non-zero (not end of string) HLT / Else halt on zero (end of string) TLS One the / Output high level in character instruction the AC to themay be multiple teleprinter C assembly instructions C#, Java System.out.println(“Hello World!”); Low Level to High Level Languages Binary: 1000110100111010100100100111 Assembly: TAD I Z STPTR / Get next character SNA / Skip if non-zero (not end of string) HLT / Else halt on zero (end of string) TLS / Output the character in the AC to the teleprinter C C#, Java System.out.println(“Hello World!”); The Java Virtual Machine (JVM) High Level Java source code Javac compiler Lower Level Java bytecode JVM for JVM for Unix Windows Unix OS Windows OS Low Level to High Level Languages In this module we will teach Java, a high level language Programs Data, and Instructions to process data Data and instructions We talk about data in terms of variables and constants We talk about instructions in terms of methods and functions We are going to look at both. Data -> variables You have already met the concept of variables in In programming, variables do not basic algebra: only represent numbers; they can represent any data we use in our y = mx + c code. y, m, x, and c all represent numbers. Any That could be numbers, text, numbers. colours, mp3s, movies, people etc They can have any value (within the constraints And we give them better names: of the equation!). That means their values can squareRoot, mainText, myBlue, vary. so they are called variables. youngerBrother etc We can also represent data with constants. what do you think is the difference between a variable and a constant? Data Data is stored in memory. x = 8; We assign that data to a y = 4; variable. = is an operator. It is the assignment operator. It assigns the data on the right to the variable on the left. Data Types All digital data is stored as binary 1000110110001 What does 1000110110001 mean? Depends on the context! It helps the computer to know what type of data it is. Is it: number text image mp3 ? In programming, we have data types so that the computer knows how to deal with the data. A (not Java) Program (in Pseudocode) to add 2 numbers! //This is a comment. The computer ignores it //Declare the data: This is a variable of type Number number1; Number. Its name is Number number2; number1. It has no value Number answer; yet. //Input values: Here we assign (give) a number1 = 10; value of 10 to number1. number2 = 7; This is called initialising the variables. //Process data: answer = number1 + number2; This is still pretty much how //Output answer: all languages work at the basic level print(answer); A (not Java) Program to add 2 numbers! //Declare the data: //Declare the data: Number number1; Number x; Number number2; Number y; Number answer; Number z //Input values: //Input values: number1 = 10; x = 10; number2 = 7; y = 7; //Process data: //Process data: answer = number1 + number2; z = x+ y; //Output answer: //Output answer: print(answer); print(z); Number types In computers, whole numbers and fractional numbers are stored in memory differently There are other differences in the way computers can store numbers, too. This means there are a number of different data types for programmers. You will learn about some of these next week. The main two types are: Integer – whole numbers written as int double - floating point, fractional numbers Int x = 769 double y = 3.14 Adding two whole numbers in Java int number1; int number2; Declare the variables as integers int answer; number1 = 7; Initialise the variables number2 = 12; answer = number1 + number2; Process/calculate System.out.println("Answer = "+answer); Output Will display “Answer = 19” Adding two whole numbers in Java declares type and name of variable: int number1; an integer, therefore int int number2; int answer; declares name of variable, number1 number1 = 7; Initialises the variables number2 = 12; number 1 = 7 answer = number1 + number2; Process/calculate System.out.println("Answer = "+answer); System out is a way of displaying the output. Displays everything in (brackets) on screen Will display “Answer = 19” Dividing two numbers in Java double number1; double number2; Declare the variables as doubles, as double answer; you need to use decimals number1 = 1.0; Initialise the variables number2 = 2.0; answer = number1 / number2; Process/calculate System.out.println("Answer = "+answer); Output Will display “Answer = 0.5” String - Text data types There are a number of ways to represent text values. The most common is to use String String myName = “Andy”; You will use other data types later in the semester Strings can be combined String x = “X value”; We can also store True/False information Boolean (means true or false) Binary data! Cannot store anything except this Very useful for checking if conditions are met Its size varies; it only uses 1 bit, but can take up to 32 bits of memory! boolean bool = false; boolean labThisWeek = true; Boolean data types A Java program with text: String String firstName; String secondName; Declare the variables String fullName; firstName= “Andrew”; Initialise the variables secondName = “Abel”; Process/calculate fullName = firstName+ secondName; System.out.println(”My full name is = ” + ” “ + fullName); Output: My full name is Andy Joe String – Escape Characters? Strings are easy to use String s = “hello” But what if we want to use characters that cause problems For example, what if we want to display the quote marks: String s = “”Hello””? Try it in Java…it will break the program. To display the “, we need to add a back slash (\) to escape it. String s = ““\”Hello \”” Likewise, if we want to insert a tab String s = “\t” Or a new line String s = “Hello \n Everyone” Or a backslash! String s = “C\\files\\java.exe” \t - tab. \b - backspace (a step backward in the text or deletion of a single character). List of Escape \n - new line. Characters \' single quote. \" double quote. \\ backslash. A full Java Program Putting it all together in IntelliJ A Java command Previously we showed lines of code String firstName; To run in Java we need to use a semi-colon at the end of each line We have to adhere to Java syntax rules (you will learn it!) We also showed code blocks for int, double, and String processing A complete Java program is more complex A Java program with text: String public class Week1 { The name of the class is Week1 main is a method. The main method public static void main(String[] args) { is where all Java programs start String firstName; String secondName; String fullName; firstName= “Joe”; Code blocks are enclosed within curly secondName = “Lewis”; brackets { } fullName = firstName+ secondName; System.out.println(”My full name is = ” + ” “ + fullName); Close brackets– end of main } } Close brackets – end of class Week1 Other Java syntax Class – the start of what you are working on. Will be covered in future weeks, for now, don’t worry about it! {Curly Brackets} – {Blocks of code are enclosed within these. If you open one, you must close it!} Methods – A set of instructions, { enclosed in curly brackets} that can be referred to. Will be discussed in future weeks Basic Java Syntax A Java program to add two numbers package week1; The name of the class is Week1 public class Week1 { main is a method. It is the entry point for all Java programs public static void main(String[] args) { int number1; int number2; int answer; number1 = 7; number2 = 12; answer = number1 + number2; System.out.println("Answer = "+answer); } Close braces – end of main } Close braces – end of class Week1 A Java program to divide two numbers package week1; The name of the class is Week1 public class Week1 { main is a method. It is the entry point for all Java programs public static void main(String[] args) { double number1; double number2; double answer; number1 = 1.0; number2 = 2.0; answer = number1 / number2; System.out.println("Answer = "+answer); } Close braces – end of main } Close braces – end of class Week1 Console Output Previous examples have worked with ints and Strings An additional command was used to display information System.out.println() Anything within the (brackets) will be displayed to the user as program output System.out.println(“Andrew”); System.out.println(“Joe”); Will display “Andrew” Can also display variables : Will display 33 int out = 33; System.out.println(out); Can combine variables and text: Will display “Result33” int out = 33; System.out.println(“Result” + out); Console Output Can add multiple variables Will display “Andy is 35 years old” Note the spaces int out = 35; System.out.println(“Andy is “ + out + “ years old”); Can also use print rather than println: Will display Result System.out.print(“Result”); Difference between println() and print() Println will go on to a new line, print will not Will display System.out.println(“Result”); “Result System.out.println(“Result”); Result” Will display “ResultResult” System.out.print(“Result”); System.out.print(“Result”); Summary Introduction to CPT105 Java Virtual Machine Bits and Bytes High and Low Level Languages Data Types in Java Console output Programming In IntelliJ

Use Quizgecko on...
Browser
Browser