Objective: Create a Java program that will demonstrate the understanding of variable declaration and initialization, as well
Question:
Objective: Create a Java program that will demonstrate the understanding of variable declaration and initialization, as well as arithmetic operations.
Related SLOs:
SLO #1: Use an appropriate programming environment to design, code, compile, run, and debug computer programs.
SLO #4: Demonstrate working with primitive data types, strings, and arrays.
Instructions:
Using jGRASP, write a Java program named LastnameFirstname02.java, using your last name and your first name, that does the following:
Declare and print the values of 5 variables, using each of the following data types: int, double, char, boolean, String
In other words, you should use 1 int variable, 1 double variable, 1 char variable, etc.
When printing the value of your variables, use it in a sentence.
In the example below, the sentence "My dog's age is 5." prints the int variable with a value of 5
Important: Do not simply print an entire sentence -- you need to print the variable as part of your sentence.
Create a constant, then perform an arithmetic operation using the value of the constant
Remember what a constant is used for: values you don't expect to change
In the example below, a constant value is used to convert 8 inches into centimeters
Note: You can have similar output to the example below, but feel free to write your own text!
Remember to always begin your code with the following documentation comments:
/**
* Short description of the program.
*
* @author Doe John
* @assignment ICS 111 Assignment 02
* @date 01/17/2023
* @bugs Short description of bugs in the program, if any.
*/
Expected Output:
This is an example of what your program should output:
----jGRASP exec: java DoeJohn2
This sentence prints a char variable and String variable:
My dog's age is 5. His name is Snoopy!
This sentence prints a double variable:
A bubble tea costs $4.95.
The second sentence prints a boolean variable:
Is computer science cool, true or false?
Hmmmmm... I'd have to say.... true!!!
The first sentence prints an int variable.
The second sentence uses the int value and constant for an arithmetic operation:
Snoopy's tail is 8 inches long.
This is equivalent to 20.32 centimeters!
----jGRASP: operation complete.
Criteria:
-correct creation and use of each type of variable
-correct creation and use of a constant in an arithmetic operation
-output that clearly explains to the user what is happening in the program
-avoid miscellaneous mistakes, bugs, or problems
--------------------------------------------------------------------------------------------------------
My current java code is below but I'm getting a error message: ----jGRASP exec: javac -g WilliamsStanley02.java
WilliamsStanley02.java:1: error: illegal character: 'ufeff'
?/**
^
1 error
Any idea why I keep getting his error? I need help fixing this. Here is the code below:
/**
* A program that demonstrates variable declaration, initialization, and arithmetic operations in Java.
* It uses five different data types, prints the values of the variables in sentences, creates a constant, and performs an arithmetic operation using the constant.
* It also has proper documentation comments and no bugs.
*
* @author Williams, Stanley
* @assignment ICS 111 Assignment 02
* @date 01/18/2024
* @bugs None
*/
public class WilliamsStanley02 {
public static void main(String[] args) {
// declare and initialize variables of different data types
int age = 39; // an int variable
double price = 9.99; // a double variable
char grade = 'A'; // a char variable
boolean happy = true; // a boolean variable
String name = "Stanley"; // a String variable
// print the values of the variables in sentences
System.out.println("My name is " + name + ".");
System.out.println("I am " + age + " years old.");
System.out.println("I got " + grade + " in my last exam.");
System.out.println("I am happy: " + happy + ".");
System.out.println("I bought a game for $" + price + ".");
// declare and initialize a constant
final double PI = 3.14; // a constant value for pi
// perform an arithmetic operation using the constant
double area = PI * 5 * 5; // calculate the area of a circle with radius 5
// print the result of the operation
System.out.println("The area of the circle is " + area + " square units.");
}
}
Java An Introduction To Problem Solving And Programming
ISBN: 9780134462035
8th Edition
Authors: Walter Savitch