New User Special Price Expires in

Let's log you in.

Sign in with Facebook


Don't have a StudySoup account? Create one here!


Create a StudySoup account

Be part of our community, it's free to join!

Sign up with Facebook


Create your account
By creating an account you agree to StudySoup's terms and conditions and privacy policy

Already have a StudySoup account? Login here

Week 6 /7 notes & Answers to Exam 1

by: Taylor Kahl

Week 6 /7 notes & Answers to Exam 1 CSC 2720

Marketplace > Georgia State University > Computer science > CSC 2720 > Week 6 7 notes Answers to Exam 1
Taylor Kahl
GPA 4.21

Preview These Notes for FREE

Get a free preview of these Notes, just enter your email below.

Unlock Preview
Unlock Preview

Preview these materials now for free

Why put in your email? Get access to more of this material and other relevant free materials for your school

View Preview

About this Document

In addition to my usual notes, I'm uploading answers and explanations to all the questions that were on exam 1. The class notes are from lectures on Thursday 9/29 and Thursday 10/6 as well as text...
Data Structures
Jaman Bhola
Class Notes
Linked Lists, nodes
25 ?




Popular in Data Structures

Popular in Computer science

This 11 page Class Notes was uploaded by Taylor Kahl on Sunday October 9, 2016. The Class Notes belongs to CSC 2720 at Georgia State University taught by Jaman Bhola in Fall 2016. Since its upload, it has received 26 views. For similar materials see Data Structures in Computer science at Georgia State University.

Similar to CSC 2720 at GSU


Reviews for Week 6 /7 notes & Answers to Exam 1


Report this Material


What is Karma?


Karma is the currency of StudySoup.

You can buy or earn more Karma at anytime and redeem it for class notes, study guides, flashcards, and more!

Date Created: 10/09/16
Notes from 9/29 and 10/6 Ch. 5: Linked Lists Lists, such as Arrays, do not “contain” data. Each index actually contains a reference to the data  for primitive data types, this reference is to the actual data  for objects, this reference references another address, where the data is stored LinkedList – a data structure made of linked nodes  No fixed size – grows or shrinks as needed  items don’t need to be shifted when inserting or deleting an item (like in ArrayLists or Vectors)  Nodes are added at run time (dynamic memory allocation) o other data structures create memory during compilation Nodes are defined in their own class  Each Node object has at least 2 variables: the data they contain and a link to another node  Linked lists are reference-based because each node references another, linking them together The items in linked lists do not have indexes like other lists do (with indexes, index 0 is the first element, index 1 is the next element and so on)  Items in linked lists can’t be accessed by a specific index o they must be accessed by list traversal – sequentially viewing each item until target is reached  without indexes, items don’t need to be shifted when inserting or deleting an item, which saves time and allows a flexible size Access Time:  In Arrays, items can be accessed immediately via their index  in reference-based lists, the time to access a node depends on how many links come before it Memory & Storage:  Memory – used as you go  Storage – reserved space for holding items  Resizing Arrays wastes storage & time  Arrays require less memory, but linked lists don’t waste storage Example Node class: public class node { int x; x is the data stored in the node node next; next is the pointer which references another node public node(int y) { x = y; next = null; if next is null, that node has no pointer public node(int y, node z) { x = y; node = z; } } Making node objects: node n1 = new node(5); 5 3 10 node n2 = new node(3, n1); node n3 = new node(10, n2); Insertion and deletion do not require items to be shifted because they simply change the links connecting items  Inserting an item – find its desired place, set 2 links, remove the original link  Deleting an item – change links to bypass item(s) to be deleted Original list: 5 10 20 50 Inserting a node: 5 10 20 50 25 Removing a node: 5 10 20 50 If an item is no longer referenced java’s garbage collection mechanism will remove it from memory  Includes any item which is no longer linked (referenced) Circularly linked list – the last node references the 1  this makes the list more secure; less risk of losing unlinked nodes Doubly linked list – each node points to the previous and next node Head node – contains no data. References the 1 node to indicate the beginning of the list If you want to store multiple data in 1 node, rather than making a separate field for each data type it’s better to put those fields into a class and store them as a single object in the node Answers for Test 1: 1. Name and briefly describe two of the three major types of errors that may result from writing, compiling and/or running a program.  Syntax/compiler error – prevents the program from compiling because of improper syntax, such as missing a semicolon or misspelling a keyword  Runtime error – program will compile but will crash or run infinitely. Examples: infinite loop; ArrayIndexOutOfBounds, dividing by zero  Logic error – program runs but doesn’t give the result you want Common mistake: exceptions are NOT errors – they are classes which the program uses to CATCH errors 2. Operating systems constitute one major category of software. What is the other major category? Applications 3. What is the name of the code that Java uses to represent characters? Unicode 4. A series of characters enclosed in double quotes is said to be a String. 5. A java .class file is made up of byte code. 6. The javac program is the compiler. What is the java program? Java OR JVM (Java Virtual Machine) are the programs that run the code after compiling. 7. Determine if the following declaration is correct. If it is not, explain why not and show how to correct it if you think it is wrong. int i = 3; j = 5; k; i + j = k; The first line is fine. The problem is in the second line. Only 1 variable can be on the left side of the assignment operator “=” int i = 3; j = 5; k; i + j = k; k = i + j; 8. Which of the following name is not a legal java name? a. CSc2310 b. _JavaRules_ c. 100bottles d. A_OK e. S_and_P_500 The answer is C. An identifier can begin with letters or underscores, but not numbers 9. Name one advantage for declaring a value as a constant value in your program.  The value only needs to be created and stored once  The value can be reused without being changed 10.What operator can be used to combine multiple data items into a single string? The + operator concatenates data into a single string. 11.Locate the error in each of the following declarations and show the easiest way to fix it. a. int students = 30, teacher = 1, class; class is a keyword and can’t be used as an identifier – rename it “klass” b. double total-balance = 0, depositFor2000; Hyphens can’t be used in identifiers – rename it “total_balance” c. double expenses, net profit, 2001revenues; Identifiers cannot begin with numbers – rename it “revenues2001” 12.Give the resulting value and data type of each of the following expressions: a. -1 + -5.5 = -6.5 (double) b. 4 – 5 – 7 = -1 – 7 = -8 (int) c. 15 / 4 * 2.0 = 3 * 2.0 = 6.0 (double) d. 11 % 2 + 13 % 2 = 1 + 1 = 2 (int) e. 4 % 6 % 9 = 4 % 9 = 4 (int) 13. What is the output of the following program? Give the exact format of the output. public class tests1 { public static void main(String[] args) { int d1 = 31; d1 = 31 int d5 = d1 % 2; d5 = 1 d1 /= 2; d1 = 15 int d4 = d1 % 2; d4 = 1 d1 /= 2; d1 = 7 int d3 = d1 % 2; d3 = 1 d1 /= 2; d1 = 3 int d2 = d1 % 2; d2 = 1 d1 /= 2; d1 = 1 System.out.println(“Answer: “ + d1 + d2 + d3 + d4 + d5); } } Output: Answer: 11111 (The variables are concatenated as a String, not added as numbers). BTW, this is an algorithm to convert a number into binary. 14.Convert the following function consisting of a cascaded if statement to a function consisting of a switch statement: private static char grade(int score) { if (score >89 && score <= 100) return ‘A’; else if (score >79 && score <= 89) return ‘B’; else if (score >69 && score <= 79) return ‘C’; else if (score >59 && score <= 69) return ‘D’; else if (score >=0 && score <= 0) return ‘F’; else { System.out.println(“Not a grade”); return ‘’; } } To make this algorithm into a switch statement, score will be used as the case and each case will return 1 letter grade. Each case must refer to 1 number, not to a range of numbers. Therefore, the range of scores for each letter grade must be converted to a single int. To do this, divide the original score by 10. Because the original score is an int, dividing by 10 will convert every score to an int of 1 through 10. Here’s the solution: private static char grade(int score) { score = score / 10; switch (score) { case 9: case 10: return ‘A’; case 8: return ‘B’; case 7: return ‘C’; case 6: return ‘D’; case 1: case 2: case 3: case 4: case 5: return ‘F’; default: System.out.println(“Not a grade”); return ‘’; } } 15. Given the following declaration for a class called Date with one constructor and one incomplete method, complete the following method (add_days) that advances and date (object) by any number of day(s) greater than or equal to zero and returns the new Date. Assume that each month has 30 days and there are not leap years. public class Date { int day, month, year; public Date() { day = 1; month = 1; year = 2000; } public Date add_days(int n) { day += n; while (day > 30) { day - = 30; month ++; } while (month > 12) { month - = 12; year ++; } Date temp = new Date(); = day; temp.month = month; = day; return temp; } } Increase day by n. day can’t be more than 30 because there are 30 days in a month. If day > 30, decrease day by 30 and add 1 to month. If month > 12, decrease month by 12 and add 1 to year. 16.Show the output of the following program. public class tryCatch { public static void main(String[] args) { double a = 0.0, b = 5.0, c; try { c = a / b; } catch (ArithmeticException e) { System.out.println(“Error in first division”); return; } System.out.println(“Finished first division”); System.out.println(“The first value of c is: “ + c); try { c = b / a; } catch (ArithmeticException e) { System.out.println(“Error in second division”); return; } System.out.println(“Finished second division”); System.out.println(“The second value of c is: “ + c); } } Output: Finished first division The first value of c is 0.0 Finished second division The second value of c is infinity Note: Dividing by the int 0 is illegal. Dividing by 0.0 IS legal and the answer equals infinity. Neither of the exceptions will be thrown 17. Given the following incomplete code that is supposed to read from an input file and count the amount of words, lines and alphabetic characters (letters only), then print them to the screen. Add code to make it work properly. import*; import java.lang.String; import java.util.*; this line has to be added to be able to use Scanner objects public class Main { public static void main(String[] args) { Scanner input = new Scanner(; System.out.println(“Enter the file input name:”); String inputFile =; System.out.println(“-------------------------------------------------------“); getinput2(intputFile); } public static void getinput2(String FileName) { String input; Scanner fileinput = null; int linecount = 0, wordcount = 0, charcount = 0; File inFile = new File(FileName); try { fileinput = new Scanner(inFile); while(fileinput.hasNext()) { input = fileinput.nextLine(); linecount ++; for (int i = 0; i < input.length(); i ++) { if (input.charAt(i) == ‘ ‘) wordcount++; if (i == input.length() – 1) wordcount ++; if (character.isLetter(input.charAt(i))) charcount ++; } } } } While fineinput still has more text, the program reads the next line and linecount increases by 1. The program increases wordcount by 1 when it reads a space character and adds another 1 at the end of the line (because there’s no space but the last word needs to be counted). charcount is increased by 1 whenever the program reads a character that is a letter. 18.You need to develop an ADT for a List Data Structure. Make a list of 5 list operations that would be required for the List Data Structure excluding the constructor(s).  Add object at end of list  Add object at a given index  Remove object at a given index  Remove an object at its first occurrence in the list  Retrieve object at a given index  Return index of an object  Return size of the list  Check if the list is empty  Check if an object is in the list 19.Describe the problem with the following recursive method and add code to correct it. Note: the method is supposed to print from n to 0. public void printNum(int n) { System.out.println(n); printNum(n – 1); } Problem: There is not terminating condition so the code will run infinitely. Solution: public void printNum(int n) { if (n >= 0) { System.out.println(n); printNum(n – 1); } } 20.Write a recursive voided function name printSquares that takes an integer parameter and print to the screen the sequence from 0 to that number (or that number to 0) and their squares. Make sure you add pre and post conditions to your method. Preconditions: This method accepts an int n as a formal parameter. It assumes that n >= 0. public void printSquares(int n) { if (n == 0) { System.out.println(“0 0”); } else { printSquares(n – 1); System.out.println(n + “ “ + n*n); } } Postconditions: This method will print, from 0 to n, the value of n and its square on the same line.


Buy Material

Are you sure you want to buy this material for

25 Karma

Buy Material

BOOM! Enjoy Your Free Notes!

We've added these Notes to your profile, click here to view them now.


You're already Subscribed!

Looks like you've already subscribed to StudySoup, you won't need to purchase another subscription to get this material. To access this material simply click 'View Full Document'

Why people love StudySoup

Jim McGreen Ohio University

"Knowing I can count on the Elite Notetaker in my class allows me to focus on what the professor is saying instead of just scribbling notes the whole time and falling behind."

Amaris Trozzo George Washington University

"I made $350 in just two days after posting my first study guide."

Jim McGreen Ohio University

"Knowing I can count on the Elite Notetaker in my class allows me to focus on what the professor is saying instead of just scribbling notes the whole time and falling behind."


"Their 'Elite Notetakers' are making over $1,200/month in sales by creating high quality content that helps their classmates in a time of need."

Become an Elite Notetaker and start selling your notes online!

Refund Policy


All subscriptions to StudySoup are paid in full at the time of subscribing. To change your credit card information or to cancel your subscription, go to "Edit Settings". All credit card information will be available there. If you should decide to cancel your subscription, it will continue to be valid until the next payment period, as all payments for the current period were made in advance. For special circumstances, please email


StudySoup has more than 1 million course-specific study resources to help students study smarter. If you’re having trouble finding what you’re looking for, our customer support team can help you find what you need! Feel free to contact them here:

Recurring Subscriptions: If you have canceled your recurring subscription on the day of renewal and have not downloaded any documents, you may request a refund by submitting an email to

Satisfaction Guarantee: If you’re not satisfied with your subscription, you can contact us for further help. Contact must be made within 3 business days of your subscription purchase and your refund request will be subject for review.

Please Note: Refunds can never be provided more than 30 days after the initial purchase date regardless of your activity on the site.