Document Details

Uploaded by Deleted User

Tags

java programming integrated development environment computer science

Full Transcript

CS109 Lab1 2024Spring Outline  1. Learn how to use an Integrated Development Environment (IDE) in writing JAVA programs  2. Practice using input and output statements.  3. Practice storing values with primitive types Learn how to use IDE in writing JAVA programs Afte...

CS109 Lab1 2024Spring Outline  1. Learn how to use an Integrated Development Environment (IDE) in writing JAVA programs  2. Practice using input and output statements.  3. Practice storing values with primitive types Learn how to use IDE in writing JAVA programs After installation, let’s run a java program in IDEA. Run IDEA, choose “create new project”. or Learn how to use IDE in writing JAVA programs Create new project. You can change the name or the Location. Now you have your first program in src. Learn how to use IDE in writing JAVA programs Run your program. Learn how to new a new JAVA program Run your program. Enter the file name and press Enter The first program You can change the red part, but memorize the rest. hint: try “psvm”. 1. You must have a class to run your program. 2. One java file can have no more than one public class, but can have more than one class. 3. “Main” is class name. You can change it. But public class name must be the same as the file name. Primitive Types Assignment Statement variable : type variable name int a; ←Declaration statement, used only once when creating variables a = 2333; ←Assignment statement, used when changing variables variable assign value Primitive Types matching: What type of value does the following constant belong to ? byte short int long float double char boolean 0 1 128 32767 1111111L 2.0 2.0f ‘a’ “a” true false byte short int long float double char boolean What type of variable can the constants above be stored in ? Output statement System.out.println(); System.out.print(); hint: try “sout” Output statement Run this program. What happened? Output statement Run this program. What happened? Try System.out.printf(“%d”,3.5678); What happened? %d for int\long, %f for float\double %.2f for float\double with two decimal places Input statement Run this program and input: Input: Helloworld Hello world Helloworld Hello world 3 3 3.0 3 What happened? Input: Hello world Hello 3 3 Input: Hello world Hello 3.0 3 Input statement Run this program and input: Helloworld Helloworld What happened? Input statement Run this program and input: Helloworld What happened? Lab exercise Write a program that prompts the user to enter the height and width of a rectangle then prints the area and perimeter of the rectangle. The area and perimeter should be printed to the nearest two decimal places. Sample output: Lab exercise Write a time converter that prompts the user to enter the number of seconds then prints the equivalent time in hours, minutes and seconds. Sample output: Tips Java is strictly case sensitive. All keywords are lowercase (eight basic types are all lowercase!!!) A java file can have multiple classes, but at most one public class. If a java file contains a public class, the file name must be the same as the public class name nextLine() and next() \ nextInt() methods should not be used together The charAt() method starts from the 0th bit of the string and pays attention to the variable type. Constants also have different types, such as 0.2f and 0.2. When calculating different types of numerical values, pay attention to the class of the operation result. When calculating different types of numerical values, pay attention to the calculation priority and order of each operator THANK YOU

Use Quizgecko on...
Browser
Browser