CSCI 114 Programming Assignment 1

- cs 114 programming assignment 1
Reading/Exercise:
- The slides I have covered in class
- The book I have referred to
- The contents (website or other links) mentioned in the Practice materials of
Instructions:
- Read all the stuff mentioned in the reading/exercise section of this
- Answer the questions in a clear, coherent, and professional manner
- You are encouraged to read books/online If you get any help from these, clearly cite the source in your writing.
- Possible helps include, but are not limited to:
- Seeing a YouTube
- Seeing a solution to the problem
- Seeing a discussion about the problem
- Discussing with someone other than your classmates in this
- Submit the assignment on
- Possible helps include, but are not limited to:
Feel free to contact me (sarker@hartford.edu) or preceptor Rahul (rkumar@hartford.edu) anytime for any confusion or difficulty in understanding.
Deliverable on blackboard:
- You need to submit to Blackboard. No other form of submission (email/hard copy) is allowed unless you have written permission from the university.
- Submit 2
- 1 PDF This will include
- Answers for all the questions
- Source code copied and pasted from your
- 1 zip file for source This will include
- All of your source code
- Instructions in a txt file on how to execute/run your source code.
Grade points and other information:
- This assignment has a total of 10 grade
- Questions 1 to 5 are worth 4 points each, totaling 2 points.
- Questions 6 to 10 are worth 1 point each, totaling 5
- Questions 11 and 12 are worth 1 point each, totaling 2
- Clear, coherent, and organized writing and proper submission according to instructions is 1 point.
- There is a bonus question at the end of this That is optional. If you can solve that, you will get an extra 5 points.
Writing Questions
Question 6: Discuss these in relation to Java.
- What is JDK?
- What is JRE?
- What is bytecode?
- What is a compiler?
- What is an interpreter?
- Is Java a compiled language or an interpreted language?
Question 7: Discuss these in relation to Java.
- What is the file extension for Java source code?
How to compile Java source code (without the help of an IDE such as IntelliJ)?
- How to run a Java program (without the help of an IDE such as IntelliJ)?
Question 8: Write the names of all the keywords ( reserved words in Java) you have used so far and their usage in Java.
Questions 9:
- How many primitive data types are in Java? What is the range of them?
- What are the possible ways to convert data in Java? Discuss with an
Question 10: What is operator precedence? Arrange all the operators in Java according to their precedence.
6
A: The JDK allows the programmer to construct Java applications that the JVM and JRE can execute and operate.
B: Is a package of tools for building and running Java programs. The Java Runtime Environment JRE is a component of the Java Software Package JDK.
C: The Java Virtual Machine’s instruction set is called bytecode, and it works similarly to an assembler.
D: The Java compiler translates that converts a developer’s file format work into a platform-independent Java file.
E: A class file is translated into code that can be performed natively on the host computer by the Java interpreter, java.
F: Since Java’s source code is initially turned into binary byte-code, it could be called both a compiled and an understood language.
7
A: The bytecode file extension is. class, while the source file extension is. java. B: You can compile Java code in the Eclipse compiler.
C: You can also run the Java code in Visual Code. 8
If Tests a true/false expression and branches accordingly
int
A data type that can hold a 32-bit signed integer
final
Indicates that a variable holds a constant value or that a method will not be overridden
float
A data type that holds a 32-bit floating-point number
public
An access specifier used for classes, interfaces, methods, and variables indicating that an item is accessible throughout the application (or where the class that defines it is accessible)
byte
A data type that can hold 8-bit data values, char
A data type that can hold unsigned 16-bit Unicode characters
long
A data type that holds a 64-bit integer
short
A data type that can hold a 16-bit integer
while
Starts a while loop 9
There are 8 data types in Java: byte 8 bits, short 16 bits, int 32 bits), long 64 bits, float 32 bits),double 64 bit, boolean true and false, char 16 bits.
Widening conversions take lower to higher precision conversion and are therefore done by the compiler, for example, of byte-short-int-long-float-double.
10
The higher an operator’s precedence, the closer it appears to the top of the table. Operators with a higher precedence are rated first, followed by operators with a lower precedence.
Implementation Questions
Question 11: Write a program that will input some number of cents (less than 100) and output the number of quarters, dimes, nickels, and pennies needed to add up to that amount.
Question 12: Create a program that will be able to create a pyramid (similar to picture 2) on the screen based on the user input. Specifically,
- Your program needs to take input from the Assume that is n. n can be anything between 1 and 100.
- Print the pyramid (similar to picture 2) on the screen with size
Helpful description:
Picture 1
This is a picture (picture 1) of a pyramid.
We, as Java programmers, often get requests from our clients to create pyramids (and many other beautiful kinds of stuff) using our program. We need to satisfy their request to get our payment.
How can we perform that?
Let me elaborate on that. As we don’t know much of CS stuff yet, to simplify the process, we will print a similar structure through our program. That means not the exact but a similar structure. Our simplified pyramid will be like this, as in picture 2:
12
public class 1Pyramid {
public static void main(String[] args){ int c= 0; // the placement of the stars
Scanner scan = new Scanner (System.in); // take the user input System.out.print (“Please Enter the size:
“); //asking user for input for the size
int p= scan.nextInt(); //storing user input value to variable size
for (int j = 1; i <= size; j++, si= 0) //runs the for loop and inner statement until the condition isn’t true
–
{
for (int blank = 1; blank
<= si
– ¡; blank++) //spaces before each star that starts the line if the condition
{
System.out.print(. “
while (c I=2 * 1 – 1) {//checks how many stars that’s being added System.out.print(“* “); //Prints out the star and the space between them. C++; //increment C
System.out.printin();
11
import java.util.Scanner; public class CountC {
public static void main(String[] args) {
Scanner stdio = new Scanner( System.in );
int qu; // For the number of quarters, to be input by the user.
int dim; // For the number of dimes, to be input by the user. int nick; // For the number of nickels, to be input by the user. int pen; // For the number of pennies, to be input by the user.
double dollar; // Total value
System.out.print(“How many quarters: “); qu = stdio.nextInt();
stdio.nextLine();
System.out.print(“How many dimes: “);
dim = stdio.nextInt(); stdio.nextLine();
System.out.print(“How many nickels: “); nick = stdio.nextInt();
stdio.nextLine();
System.out.print(“How many pennies: “); pen = stdio.nextInt();
stdio.nextLine();
dollar = (0.25 * qu) + (0.10 * di)
+ (0.05 * nick) + (0.01 * pen);
System.out.println();
System. out.print(“The total in dollars is $”); System.out.println();
}
}
Picture 2
Look carefully at this pyramid (picture 2), which uses * (multiplication or star symbol on our keyboard) to create a whole pyramid shape.
Picture-3
Look at this picture carefully. We used the 5-star (*) symbol to define the height of the pyramid. The size of the base of this pyramid is 9, which we can formulate as height * 2 -1. That means 5*2-1 = 9. We can use the number of stars for the height of the pyramid, to call it the size of the pyramid. This is a (picture 2) is a 5 size pyramid. Sometimes the client demands to be able to create not only a 5-size pyramid, but also whatever size he/she wants. May be 2 size pyramid, a 3-size pyramid, or probably a 50-size pyramid. For example, picture-4 is a 3-size pyramid. Our program should support that.
Bonus question: Hacker level.
Following question 12, you will display a pyramid just as in Picture 1. No shortcut, no simplification. Obviously, you need to show just 1 pyramid, but in its original shape with color and different sides.
- You need to draw the shapes and present
- The size and color will be based on user
– If you can solve this bonus question, you will get 5 extra points towards the overall grade.
