Question
I only need answers for 3.1, 3.4, 3.5, 4.3, 5.3, 5.5, 5.6, 6.3, 6.8 (b, c, f, h, k, m), and 7.2. 1 Submission Instructions
I only need answers for 3.1, 3.4, 3.5, 4.3, 5.3, 5.5, 5.6, 6.3, 6.8 (b, c, f, h, k, m), and 7.2.
1 Submission Instructions
Note that for each homework assignment, only some of the exercises will be graded. A list of the exercises that will be graded can be found in the Homework Assignment 1 submission link in Blackboard (BB). This does not mean that the ungraded exercise are unimportant or that you will not be tested on the material in the exercise. They are, and you will. There are two primary reasons we grade only some of the exercises: first, this course has a large enrollment so there are many assignments to be graded; second, in the accelerated time frame for online courses, we want to return graded homework exercises as quickly as we can. Despite not grading some exercises, if you wish to perform as well as you can in this course, we strongly recommend that you all of the exercises.
Most of your solutions will be typed in a PDF document. To start, a document using your favorite word processor and type exercise solutions?except for those exercises where you are asked to submit your solution in a separate file (more later). At the top of the document please include your name, your ASURITE ID (the username you use to login to MyASU and BB), your email address, and the homework assignment number, e.g. Homework 1.
For short-answer and description exercises (e.g., see Exs. 3.2, 4.1, 4.2, 5.1-5.8), please neatly type solution in your document.
Some exercises may ask you to submit an image (e.g., see Exs. 7.1-7.2). Hand-drawn images are unacceptable and will result in zero points being assigned for an exercise (the reason being that hand-drawn images are often illegible causing students to lose points). For images, use a software drawing tool, export the image in a suitable image file format, and then copy-and-paste the image file into your word processing document. Please ensure the image is sufficiently sized so that the text is readable.
Please carefully read the following sections below regarding submission of Java source code files (Exs. 3.1, 3.4, 3.5, 4.3). We will be using an automated grading script to grade these exercises, so it is extremely important that you follow the assignment instructions?especially in regard to naming?so your submission will not cause the script to fail resulting in point deductions.
For other exercises which ask you to write Java code but do not request you submit your solution in a separate Java source code file (e.g., see Exs. 3.3, 3.6, 3.7), copy-and-paste your solution from your IDE into your word processing document. Make sure to neatly format your code, i.e., indentation.
When you are done, convert the word processing document into Adobe PDF format and name the PDF file asuriteid. pdf where asuriteid is your ASURITE user id (for example, if your ASURITE user id is jsmith6 then your file would be named jsmith6.pdf).
Next, an empty folder named asuriteid and copy asuriteid.pdf to that folder. Copy any requested Java source code files to this folder (note: Java source code files are the files with a .java file name extension; do not copy the .class files as we do not need those, and if the exercise involves data files, we also do not need those).
Next, compress the asuriteid folder creating a zip archive file named asuriteid.zip. Upload asuriteid.zip to the Homework Assignment 1 submission link by the assignment deadline. Please see the Course Schedule section in BB or in the Syllabus for the deadline. Consult the Syllabus for the late and academic integrity policies.
1.1 Submitting Java Source Code Files Containing Methods. For Exs. 3.1, 3.4, 3.5?which ask you to submit Java files containing just methods (not a runnable program) that solve the problem?we want you to encapsulate the requested method within a class (i.e., in a .java file) named as requested in the exercise. Remember that Java is case-sensitive so your filenames, method names, variable names, etc. must be named exactly as requested in the homework document. Failure to do will cause you to lose points on an exercise. For example, here is what we want you to submit for Ex. 3.1, where you will complete the code in ex3_1() to solve the problem (next page).
// CLASS: Hw1_31
// AUTHOR: your name, your ASURITE username, your email address
import java.util.ArrayList; // Import required classes
public class Hw1_31 { // Remember that class name and filename have to be the same. Case matters.
public Hw1_31() { // Provide a default constructor. Some exercises may require other ctors.
}
// This is the method you are asked to write for Ex. 3.1. Name it exactly as requested.
ArrayList
// 1. Declare and instantiate an ArrayList
// 2. Write single for/while loops or a pair of for/while loops to fill list with the specified values.
// 3. Return list.
}
}
The CLASS: and AUTHOR: comment lines must be included. All other comment lines are optional but we strongly encourage
you to comment your code. In some situations, well-written comments may help us assign partial credit. Note that
the instructor indents using 4 spaces. It does not matter to us how many spaces you indent but please configure your
editor to insert spaces and not hard tabs when you hit the Tab key. Be sure for each exercise that you import the required
classes so that your class will build. It is preferable to import just the 1 class java.util.ArrayList rather than writing
import java. util.*; Give your standalone class, we will write driver routine that instantiates an object of your class
and then call ex3_1() on that object to test your solution. Do not provide a main() method or driver routine in your class:
for testing on your end, write own main() method and drivers in a class separate from Hw1_31. For these exercises,
you do not need to copy-and-paste your code into the word processing document, but the .java files
must be included in your zip archive.
1.2 Submitting Java Source Code Files for Complete Programs. For exercises which ask you to write complete programs
(see Ex. 4.3) your program must contain a main() method and any requested drivers. In general, unless the exercise
specifically asks you to write class (static) methods, all of the methods in your class(es) except for main() must be instance
methods. Shown below is the required template for the main class, i.e., the class containing the main() method, of a
complete program. For these exercises, you do not need to copy-and-paste your code into the word processing
document, but include the .java files in your zip archive.
// CLASS: MainClassName
// AUTHOR: your name, your ASURITE username, your email address
import required stuff ...; // Do not use * notation
public class MainClassName {
// Declare instance data as required...
public static void main(String[] pArgs) { // I often preface parameter variable names with a p
new MainClassName().run(); // Instantiate an object of this class and then call run() on the object
}
public MainClassName() { // Provide a default constructor.
}
// Provide other ctors as required...
// run() is where the action starts taking place. I like to say that when we leave main() and reach run(), we
// leave "static land" and enter "object land." Static (class) methods have their uses but they should rarely
// be used. Note: run() is private because it is intended to be only called from main().
private void run() {
}
// Provide other instance or class methods as required...
3 ArrayLists
3.1 See the instructions in 1.1 for what to submit for grading. This is not a complete program. Name your Java source code file Hw1_31.java. Write method named ArrayList Integer> ex3_1() that creates an ArrayListInteger> object named list and fills list with the numbers shown below (using one or a pair of for or while loops). list shall be returned from ex3_1().
0 1 2 3 4 0 1 2 3 4
3.2 Consider the ArrayListInteger> object named list containing these Integers:
list = { 1, 2, 3, 4, 5, 4, 3, 2, 1, 0 }
What are the contents of list after this loop completes?
for (int i = 1; i
list.set(i, list.get(i) + list.get(i-1));
}
3.3 Write enhanced for loop that counts how many numbers in an ArrayListInteger> object named list are negative. Print the count after the loop terminates.
3.4 See the instructions in 1.1 for what to submit for grading. This is not a complete program. Name your Java source code file Hw1_34.java. Write method named Integer arrayListSum(ArrayList Integer> pList). The method shall return the sum of the elements of pList as an Integer.
3.5 See the instructions in 1.1 for what to submit for grading. This is not a complete program. Name your Java source code file Hw1_35.java. Write method named named ArrayListInteger> arrayListCreate(int pLen, int pInitValue). The method shall create new ArrayListInteger> object which has exactly pLen elements. Each element shall be initialized to pInitValue. The method shall return the ArrayList.
3.6 Write void method named insertName() that has two input parameters: (1) pList which is an object of ArrayList String> where each element of pList is a person's name; and (2) a String object named pName. Assume the names in pList are sorted into ascending order. The method shall insert pName into pList such that the sort order is maintained.
3.7 Write void method named arrayListRemove() which has two parameters: pList is an object of the ArrayList Integer> class and pValue is an int. The method shall remove all elements from pList that are equal to pValue.
4 Text File Input/Output
4.1 Explain what happens if you try to open a file that does not exist for reading.
4.2 Explain what happens if you try to open a file that does not exist for writing.
4.3 See the instructions in 1.2 for what to submit for grading. This is complete program with one Java source code file named Hw1_43.java (your main class is named Hw1_43). Write program that prompts the user for the name of a Java source code file. The program shall read the source code file and output the contents to a new file named the same as the input file, but with a .txt file name extension, e.g., if the input file is foo.java then the output file shall be named foo.java.txt. Each line of the input file shall be numbered with a line number (formatted as shown below) in the output file. For example, if the user enters Hw1_43.java as the name of the input file and Hw1_43.java contains:
//***************************************************************
// CLASS: Hw1_43
//***************************************************************
public class Hw1_43 {
public static void main(String[] pArgs) {
}
public Hw1_43() {
}
}
then the contents of the output file Hw1_43.java.txt would be:
[001] //***************************************************************
[002] // CLASS: Hw1_43
[003] //***************************************************************
[004] public class Hw1_43 {
[005] public static void main(String[] pArgs) {
[006] }
[007] public Hw1_43() {
[008] }
[009] }
Hint: To print an integer as shown above in a field of width 3 with leading 0's use the Java API PrintWriter.printf() method with a format specifier of %03d. The % symbol tells the compiler this is a format specifier, 3 is the field width for the integer line number, the 0 preceding 3 tells the compiler to output leading 0's rather than leading spaces if the line number does not take up the entire field width, and d tells the compiler that we are printing an integer).
5 Exceptions and Exception Handling
5.1 Explain the difference between throwing an exception and catching an exception, i.e., explain what happens when an exception is thrown and when one is caught?
5.2 Explain what a checked exception is. Give one example.
5.3 Explain what an unchecked exception is. Give one example.
5.4 Which type of uncaught exceptions must be declared with the throws reserved word in a method header?
5.5 Why don't you need to declare that your method might throw an IndexOutOfBoundsException?
5.6 Is the type of the exception object that gets thrown always the same as the exception type declared in the catch clause that catches the exception? If not, why not?
5.7 What is the purpose of the finally clause? Give an example of how it can be used.
5.8 Which exceptions can the next() and nextInt() methods of the Scanner class throw? Are they checked exceptions or unchecked exceptions?
6 Objects and Classes
6.1 Explain how an instance method differs from a class method (static method).
6.2 Explain what happens if we write class named C but do not implement any constructors in C.
6.3 (a) In a static method, it is easy to differentiate between calls to instance methods and calls to static methods. How
do you tell the method calls apart? (b) Why is it not as easy to distinguish between calls to instance and static
methods which are called from an instance method?
6.4 Explain what happens when this application runs and why.
public class C {
private int x;
private String s;
public static void main(String[] pArgs) {
new C();
}
public C() {
x = s.length();
System.out.println("x = " + x);
}
}
6.5 See the instructions in 1.2 for what to submit for grading. This is complete program with two Java source code files
named Hw1_66.java and C.java. Note: Individually, Exs. 6.5 and 6.6 will not be graded, but you must complete
them so you may submit your complete Java program for Ex. 6.7. Create class named C in C.java and write the
class declaration for C that declares: (1) a private int instance variable named mX; (2) a private int class variable
named mY initialized to 0; (3) a private int class constant named A which is equivalent to 100; (4) a public int class
constant named B which is equivalent to 200; (5) public accessor and mutator methods for mX named getX() and
setX(); (6) public accessor and mutator methods for mY named getY() and setY(); (7) a constructor that has one int
input parameter named pX which calls setX() to initialize mX to pX; (8) a default constructor that calls C(int) to
initialize mX to -1.
6.6 Continuing the previous exercise. Createa class named Hw1_66 in Hw1_66.java. This is the main class for the program:
see 1.2 for the required template. There is an exception to the template for this exercise: omit the run()
method and in main() do not instantiate an object of the class and call run() on the object; rather, for Exercises
6.6-6.8 all of the requested statements will be written in main(). Within main() writea statement which instantiates
a C object named cObj1 by calling the default constructor.
6.7 Continuing the previous exercise. Write statement in main()?below the statement you wrote for Ex. 6.6?that
instantiates another C object named cObj2 by calling the second constructor to initialize the mX instance variable to
10.
6.8 Continuing the previous exercise. Within main(), are the following statements syntactically legal, i.e., do they compile?
If so, describe what happens when the statement is executed. If not, explain why the statement is syntactically
illegal. You may write these statements in main() to solve this exercise, but do not include these statements in the
Java file you submit for grading.
(a) int a1 = C.mX;
(b) int a2 = C.mY;
(c) int a3 = C.A;
(d) int a4 = C.B;
(e) cObj1.C(20);
(f) int a5 = cObj1.getX();
(g) cObj1.setX(20);
(h) cObj2.setX(cObj1.getX());
(i) int a6 = C.getX();
(j) C.setX(20);
(k) int a7 = cObj1.getY();
(l) cObj1.setY(20);
(m) int a8 = C.getY();
(n) C.setY(20);
6.9 Continuing the previous exercise. Suppose we add these two methods to the declaration of class C. For each assignment
statement, if it is legal (compiles) explain what happens when the statement is executed. For each assignment
statement that is illegal (syntax error) explain why the code is illegal. Do not include these methods in the C.java
file that you submit for grading.
public void f() {
mX = 0;
mY = 0;
}
public static void g() {
mX = 0;
mY = 0;
}
7 Object Oriented Design and UML Class Diagrams
7.1 Note: See 1 Submission Instructions for the instructions regarding images in exercise solutions. Below is the code for
a Java class named C. Using a UML class diagram drawing tool, draw the UML class diagram that corresponds to
this code. There are several free and open-source tools for drawing UML diagrams. Two reasonably simple-to-use and
cross-platform (Windows, Mac, Linux) tools are:
Umlet - http://www.umlet.com
Violet - http://alexdp.free.fr/violetumleditor
The instructor has been using Umlet for all of the class diagrams he has drawn for the lecture notes and he highly
recommends it as it is very easy to learn how to quickly draw a nice-looking class diagram. Using Umlet, you can
export your diagram as an image file by selecting File | Export As... on the main menu. We recommend you export
the diagram as an EPS (Encapsulated Postscript) image and then copy-and paste the EPS image into your word
processing document (I do not know if it is just Libre Office or if this would happen in MS Office as well, but I find
that EPS images look significantly better and are more readable when pasted into a Libre Office document than
PNG or JPG images).
public class C {
public static final int CONST1 = 100;
private int mX;
private double mY;
private String mZ;
public C() { this(0, 0, ""); }
public C(int pX, double pY, String pZ) { setX(pX); setY(pY); setZ(pZ); }
public int getX() { return mX; }
public int getY() { return mY; }
private String getZ() { return mZ; }
public void setX(int pX) { mX = pX; }
public void setY(int pY) { mY = pY; }
private void setZ(int pZ) { mZ = pZ; }
}
7.2 Note: See 1 Submission Instructions for the instructions regarding images in exercise solutions. On the next page is a UML class diagram that shows several classes that are associated in various ways. This class diagram is included in the Homework Assignment 1 zip archive as an Umlet file named class-relation.uxf so if you use Umlet, you can load and modify this diagram. If you are using a different UML class diagramming tool, then you will have to create the diagram from scratch. (a) Note that in Course there is an instance variable mRoster which is an instance of Roster. When a Course object is deleted, the Roster instance mRoster will also be deleted. Given that, what type of class relationship, if any, exists between Course and Roster? If there is a relationship, modify the diagram to indicate the relationship and label each end of the relationship with a multiplicity. (b) Note that in Roster there is an instance variable mStudents which is an ArrayList of Student objects. When a Roster object is deleted, mStudents is also deleted, but each Student object within mStudents is not deleted. Given that, what type of class relationship, if any, exists between Roster and Student. If there is a relationship, modify the diagram to indicate the relationship and label each end of the relationship with a multiplicity. (c) What type of class relationship, if any, exists between Student and UndergradStudent? If there is a relationship, modify the diagram to indicate the relationship and label each end of the relationship with a multiplicity. (d) What type of class relationship, if any, exists between Student and GradStudent? If there is a relationship, modify the diagram to indicate the relationship and label each end of the relationship with a multiplicity. (e) What type of class relationship, if any, exists between UndergradStudent and GradStudent? If there is a relationship, modify the diagram to indicate the relationship and label each end of the relationship with a multiplicity.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started