1 of 34

2 of 34

AP Computer Science

Introduction to Java

Credit: Slides are modified with permission from Barry Wittman at Elizabethtown College

This work is licensed under an Attribution-NonCommercial-ShareAlike 3.0 Unported License

3 of 34

First Program

4 of 34

Hello World

  • Ignore the keyword public for now
  • The keyword class announces that a new class is about to be named
  • Hello is the name of the class
    • this means the filename of the program must be Hello.java
  • The braces mark the beginning and end of the contents of class Hello
  • For each open brace there must be a corresponding closing brace.

public class Hello

{

}

5 of 34

6 of 34

7 of 34

Hello World

  • The previous empty class will compile, but it will not run
  • We need to give our class a starting point
  • The starting point for any Java program is the main() method
  • The main() method is required as shown below.

public class Hello

{

public static void main(String[] args)

{

}

}

8 of 34

9 of 34

Hello World

  • The previous program will run, but it will not do anything
  • Now, we add a print statement to our main()

public class Hello

{

public static void main(String[] args)

{

System.out.println("Hello world!");

}

}

10 of 34

11 of 34

Hello World

  • String[] args is a parameter for an array of Strings passed when you are running your application through command line in the OS

public class Hello

{

public static void main(String[] args)

{

System.out.println("Hello world!");

}

}

12 of 34

13 of 34

Write, compile, and run the Hello World program in both

jGRASP and repl.it.

14 of 34

jGRASP

15 of 34

16 of 34

17 of 34

18 of 34

19 of 34

Create Your Own in jGRASP

20 of 34

repl.it

21 of 34

What is repl.it?

repl.it is a simple yet powerful online Integrated Development Envorinment (IDE), Editor, Compiler, Interpreter, and REPL. Code, compile, run, and host in 50+ programming languages.

  • REPL = Read-Eval-Print Loop - is a simple, interactive computer programming environment that takes user inputs, evaluates/executes them, and returns the result to the user.

22 of 34

Sign Up

Beware !!!

If you use your AISD e-mail address, be sure to save your password, as repl.it will be unable to email you a password reset.

Use any user name you like, but first and last name of registration must follow a specific pattern shown later in this presentation.

23 of 34

Create a new repl for your program

Default repl (i.e. program) name - change it here now or on the repl page later.

24 of 34

main() method must be in Main class in Main.java

your program

program output

run your program

public class Main

25 of 34

26 of 34

Join these repl.it classes

https://repl.it/classroom/invite/9YBbNS5

  • AP CS repl.it class

https://repl.it/classroom/invite/svmrzxO

  • AP CS Exercises repl.it class

27 of 34

  1. For first name enter period as P# (ie period 1 is P1).
  2. For last name enter last name, then append capitalized first letter of first name. (Jane Smith would be SmithJ)

28 of 34

Go into the AP CS repl.it class and select the

01. Intro01a:Hello World! assignment

29 of 34

enter/finish program here

program output shown here

instructions from teacher

go back

30 of 34

program output shown here

run your program

run the teacher's tests

Write the program, run & test it

31 of 34

Programming Errors

forgot the ;

misspelled println

run your program

run your program

32 of 34

33 of 34

When all the tests pass & you are ready to turn the assignment in click on submit

34 of 34

Sometimes the teacher will provide a model solution