Which of the following must first be applied to programs written in the Java high

What do Android phones, Minecraft, and Netflix have in common? They’re all programmed in Java! Many of the apps you use in an Android phone or tablet are written in Java. If you’ve used App Inventor before, those apps are translated to Java before they are run on a phone or tablet. Netflix uses Java for some of its software too. Java is a programming language that is used worldwide to create software that we all use.

The following video introduces this first lesson in CSAwesome.

1.2.1. First Java Program

Every program in Java is written as a class. Java is an object-oriented language and we’ll learn more about classes and objects in Unit 2. Inside the class, there can be a main method that starts the program. When you ask the Java run-time to run a class, it will always start execution in the main method. Here is the template for a simple Java program with a main method:

public class MyClass
{
   public static void main(String[] args)
   {
      // Put your code here!
   }
}

Note

In Java every open curly brace

        public class ThirdClass
{
---
   public static void main(String[] args)
   {
   ---
      System.out.println("Hi my friend!");
      ---
   }
   ---
}
        
1 must have a matched close curly brace
        public class ThirdClass
{
---
   public static void main(String[] args)
   {
   ---
      System.out.println("Hi my friend!");
      ---
   }
   ---
}
        
2. These are used to start and end class definitions and method definitions.

Coding Exercise: Click on the button below to have the computer execute the

        public class ThirdClass
{
---
   public static void main(String[] args)
   {
   ---
      System.out.println("Hi my friend!");
      ---
   }
   ---
}
        
3 method in the following class. Then, change the code to print your name. Be sure to keep the starting
        public class ThirdClass
{
---
   public static void main(String[] args)
   {
   ---
      System.out.println("Hi my friend!");
      ---
   }
   ---
}
        
4 and ending
        public class ThirdClass
{
---
   public static void main(String[] args)
   {
   ---
      System.out.println("Hi my friend!");
      ---
   }
   ---
}
        
4. Click on the button to run the modified code. If you revisit this page later and login, click on Load History and move the bar above it to see your previous code changes.

Run this code to see the output below it. Then change the code to print your name, for example “Hi Pat!”, and run again.

You can copy the Java source code shown in this book into a file and save it if you want to run it locally in an integrated development environment (IDE) on your local computer (see section 1.1. for different IDEs). You must name the file the same name as the class name with “.java” as the extension. All code (programs) in Java must be defined inside a class in a source file, and the name of the class must match the file name.

1.2.2. Print Commands

Java has two different print commands to print output to the screen:

  • System.out.println(value) : prints the value followed by a new line (ln)

  • System.out.print(value) : prints the value without advancing to the next line

        public class ThirdClass
{
---
   public static void main(String[] args)
   {
   ---
      System.out.println("Hi my friend!");
      ---
   }
   ---
}
        
6 prints out the characters between the first
        public class ThirdClass
{
---
   public static void main(String[] args)
   {
   ---
      System.out.println("Hi my friend!");
      ---
   }
   ---
}
        
4 and the second
        public class ThirdClass
{
---
   public static void main(String[] args)
   {
   ---
      System.out.println("Hi my friend!");
      ---
   }
   ---
}
        
4 followed by a new line. The
        public class ThirdClass
{
---
   public static void main(String[] args)
   {
   ---
      System.out.println("Hi my friend!");
      ---
   }
   ---
}
        
9 is called a string literal, and it can have zero to many characters enclosed in starting and ending double quotes.

Run this code to see the output below it. How would you change it to print the ! on the same line as Hi there keeping all 3 print statements?

Most command keywords in Java must be in lowercase, but class names such as System and String are capitalized. Commands in Java must end with a semicolon (;). Think of the semicolon (;) in Java like a period (.) in English. You use a semicolon (

        public class FourthClass
{
---
public Class FourthClass
{                         #paired
---
   public static void main(String[] args)
   {
   ---
   public static void main()
   {                         #paired
   ---
      System.out.println("Hi there!");
      ---
      System.out.println("Hi there!") #paired
      ---
   }
   ---
}
        
0) to show the end of a Java statement, just the way you use a period (.) to show the end of an English sentence. You will not be penalized on the exam if you forget the semicolon. However, your programs won’t run without it.

1.2.3. Syntax Errors and Debugging

Computers don’t actually speak Java so we have to compile (translate) Java source files that we write into class files which is code that a computer can understand and run. In this e-book, the Java code is actually being sent to a Java server to compile and run, and the output is sent back to show on the same page.

Syntax errors are reported to you by the compiler if your Java code is not correctly written. Examples of syntax errors are a semicolon

        public class FourthClass
{
---
public Class FourthClass
{                         #paired
---
   public static void main(String[] args)
   {
   ---
   public static void main()
   {                         #paired
   ---
      System.out.println("Hi there!");
      ---
      System.out.println("Hi there!") #paired
      ---
   }
   ---
}
        
0 missing or if the code has a open curly brace
        public class ThirdClass
{
---
   public static void main(String[] args)
   {
   ---
      System.out.println("Hi my friend!");
      ---
   }
   ---
}
        
1 or open quote
        public class ThirdClass
{
---
   public static void main(String[] args)
   {
   ---
      System.out.println("Hi my friend!");
      ---
   }
   ---
}
        
4, but no close curly brace
        public class ThirdClass
{
---
   public static void main(String[] args)
   {
   ---
      System.out.println("Hi my friend!");
      ---
   }
   ---
}
        
2 or close quote
        public class ThirdClass
{
---
   public static void main(String[] args)
   {
   ---
      System.out.println("Hi my friend!");
      ---
   }
   ---
}
        
4. Informally, a syntax error is called a bug, and the process of removing errors is called debugging. An early computer science pioneer Grace Hopper documented a real bug, a moth that flew into a computer in 1947!

Figure 2: Grace Hopper’s log showing a real bug, 1947.

The compiler tries to run your code, but if your code has syntax errors, you will see error messages displayed below the code. Compiler error messages will tell the line number that the compiler found the error and the type of error. The error messages are not always easy to understand and sometimes the actual error is before the line that the compiler says is the problem.

Watch the following video to see that all coders get bugs. Debugging is a normal part of coding. It can be frustrating at times, but you will get better at it with practice! Sometimes another pair of eyes really helps, so ask a friend if you get stuck or try explaining your code line by line to someone or even a rubber duck. Rubber duck debugging is a lot of fun!

Let’s practice debugging some code!

Check Your Understanding: Mixed up programs

The following has all the correct code to print out “Hi my friend!” when the code is run, but the code is mixed up. Drag the blocks from left to right and put them in the correct order. Click on the “Check Me” button to check your solution. You will be told if any of the blocks are in the wrong order or if you need to remove one or more blocks. After three incorrect attempts you will be able to use the Help Me button to make the problem easier.

        public class ThirdClass
{
---
   public static void main(String[] args)
   {
   ---
      System.out.println("Hi my friend!");
      ---
   }
   ---
}
        

The following has all the correct code to print out “Hi there!” when the code is run, but the code is mixed up and contains some extra blocks with errors. Drag the needed blocks from left to right and put them in the correct order. Click on the “Check Me” button to check your solution.

        public class FourthClass
{
---
public Class FourthClass
{                         #paired
---
   public static void main(String[] args)
   {
   ---
   public static void main()
   {                         #paired
   ---
      System.out.println("Hi there!");
      ---
      System.out.println("Hi there!") #paired
      ---
   }
   ---
}
        

Coding Exercise: Compile Time Error 1

Click on the button below to try and run the following code. Look for an error message after the code. This is called a compile time error because it is an error detected by the compiler.

What is wrong? Can you fix it? The error message will tell you the line number that it thinks is causing the error (

        public class FourthClass
{
---
public Class FourthClass
{                         #paired
---
   public static void main(String[] args)
   {
   ---
   public static void main()
   {                         #paired
   ---
      System.out.println("Hi there!");
      ---
      System.out.println("Hi there!") #paired
      ---
   }
   ---
}
        
6). Check line 5 to make sure that everything looks correct. One good thing to check is that all
        public class ThirdClass
{
---
   public static void main(String[] args)
   {
   ---
      System.out.println("Hi my friend!");
      ---
   }
   ---
}
        
1 have a matching
        public class ThirdClass
{
---
   public static void main(String[] args)
   {
   ---
      System.out.println("Hi my friend!");
      ---
   }
   ---
}
        
2 and all
        public class FourthClass
{
---
public Class FourthClass
{                         #paired
---
   public static void main(String[] args)
   {
   ---
   public static void main()
   {                         #paired
   ---
      System.out.println("Hi there!");
      ---
      System.out.println("Hi there!") #paired
      ---
   }
   ---
}
        
9 have a matching
public class MyClass
{
   public static void main(String[] args)
   {
       System.out.println("Hi there!");
   }
}
0 and all starting
        public class ThirdClass
{
---
   public static void main(String[] args)
   {
   ---
      System.out.println("Hi my friend!");
      ---
   }
   ---
}
        
4 have a ending
        public class ThirdClass
{
---
   public static void main(String[] args)
   {
   ---
      System.out.println("Hi my friend!");
      ---
   }
   ---
}
        
4 as well. Try putting in the missing symbol and run again. This is called debugging.

Coding Exercise: Compile Time Error 2

Click on the button below to try and run the following code. Look for an error message after the code. What is wrong this time? Can you fix it? One good thing to check is that all

        public class ThirdClass
{
---
   public static void main(String[] args)
   {
   ---
      System.out.println("Hi my friend!");
      ---
   }
   ---
}
        
1 have a matching
        public class ThirdClass
{
---
   public static void main(String[] args)
   {
   ---
      System.out.println("Hi my friend!");
      ---
   }
   ---
}
        
2 and all
        public class FourthClass
{
---
public Class FourthClass
{                         #paired
---
   public static void main(String[] args)
   {
   ---
   public static void main()
   {                         #paired
   ---
      System.out.println("Hi there!");
      ---
      System.out.println("Hi there!") #paired
      ---
   }
   ---
}
        
9 have a matching
public class MyClass
{
   public static void main(String[] args)
   {
       System.out.println("Hi there!");
   }
}
0 and all starting
        public class ThirdClass
{
---
   public static void main(String[] args)
   {
   ---
      System.out.println("Hi my friend!");
      ---
   }
   ---
}
        
4 have a ending
        public class ThirdClass
{
---
   public static void main(String[] args)
   {
   ---
      System.out.println("Hi my friend!");
      ---
   }
   ---
}
        
4 as well.

Coding Exercise: Compile Time Error 3

Click on the button below to try and run the following code. What is wrong this time? Can you fix it? After you fix the first error, you may encounter a 2nd error! Fix that one too! Hints: How do you end a command in Java? Also, check for capitalization.

Did you remember that System is capitalized in System.out.println? Did you find the missing semicolon?

1.2.5. Debugging Challenge

In this course, you are encouraged to work together in pairs to complete the programming challenges. Pair programming is a successful software development technique where two programmers work together at one computer. One, the driver, types in code while the other, the navigator, gives ideas and feedback. The two coders switch roles frequently. If you’re working alone, you may want to explain the code to a rubber duck or another toy using Rubber duck debugging.

Working in pairs, debug the following code. Can you find all the bugs and get the code to run?

1.2.6. Summary

  • A basic Java program looks like the following:

public class MyClass
{
   public static void main(String[] args)
   {
       System.out.println("Hi there!");
   }
}

  • A Java program starts with public class NameOfClass { }. If you are using your own files for your code, each class should be in a separate file that matches the class name inside it, for example NameOfClass.java.

  • Most Java classes have a main method that will be run automatically. It looks like this: public static void main(String[] args) { }.

  • The System.out.print() and System.out.println() methods display information given inside the parentheses on the computer monitor.

  • System.out.println moves the cursor to a new line after the information has been displayed, while System.out.print does not.

  • A string literal is enclosed in double quotes (’’ ‘’).

  • Java command lines end in ; (semicolon). { } are used to enclose blocks of code. // and

    public class MyClass
    {
       public static void main(String[] args)
       {
           System.out.println("Hi there!");
       }
    }
    
    9 are used for comments.

  • A compiler translates Java code into a class file that can be run on your computer. Compiler or syntax errors are reported to you by the compiler if the Java code is not correctly written. Some things to check for are ; at end of command lines, matching { }, (), and “”.

    Which of the following must first be applied to programs written in the Java high

    High-level language programs must be translated into machine language before they can be executed.

    Which among these steps comes first in programming in Java?

    Identify the Problem.
    Requirements. The first step is to examine the problem carefully to try to identify what qualifies as a solution. ... .
    Specification. The second step is to then look at the list of requirements and to decide exactly what your solution should do to fulfil them. ... .
    Coding. ... .
    Compiling. ... .
    Debugging..

    How to write first program 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 "javac MyProgram. java" in the terminal window..
    Execute (or run) it by typing "java MyProgram" in the terminal window..

    Which of the following is the first line of every Java program?

    class definition - Your java programs will always start with a class definition. Begin with the word "class" followed by the name of the program. Use curly braces to start and end the class definition. In the example shown here the name of the program would be HelloWorld.