{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Spring08_PracticeFinalExam

Spring08_PracticeFinalExam - CS1316 Spring 2008 Practice...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
CS1316 Spring 2008 Practice Exam Final NAME: ___________________________________ PRISM ID:_______________________________ 1 Part I: Random Generators and Picture Methods Using Math.random() or the Random class, construct a method to solve the following problem. Write a new method in AdvancedPicture that will randomly change the pixels of the AdvancedPicture . There is a 40% chance that the pixel will have the red value maximized. 20% chance for the green value. 15% chance for the blue value. 25% chance that all green, blue and red values are set to 0. (Hint: Max value is 255). public void randomizePixels() public void grayscale(){ Pixel[] pixels = this.getPixels(); for (int i = 0; i < pixels.length; i++){ int intensity = (int) ((pixel[i].getRed() + pixel[i].getGreen() + pixel[i].getBlue()) / 3); pixel[i].setColor(new Color(intensity,intensity,intensity)); } }
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
CS1316 Spring 2008 Practice Exam Final NAME: ___________________________________ PRISM ID:_______________________________ 2 Part II: Random Generators and LinkedList Methods Using Math.random() or the Random class, construct a method to solve the following problem. StudentList.java import java.util.Random; public class StudentList { private Random gen = new Random(); private StudentNode head; public StudentList(StudentNode head) {this.head = head;} public StudentNode getHead() {return head;} public void setHead(StudentNode head) {this.head = head;} public void add(StudentNode student){ if (size() == 0) head = student; else last().setNext(student);} public StudentNode last(){ if (size() == 0) return null; StudentNode current = head; while (current.getNext() != null) current = current.getNext(); return current;} public int size(){ int size = 0; StudentNode current = head; while (current != null){ size++; current = current.getNext();} return size;} public boolean insertAfter(StudentNode node, StudentNode newStudent){ StudentNode current = head; while (current != null){ if (current.equals(node)) break; current = current.getNext(); } if (current == null) return false; else{ StudentNode oldNext = node.getNext(); node.setNext(newStudent); newStudent.setNext(oldNext); return true;} } public String toString(){ String ret = ""; StudentNode current = head; while (current != null){ ret = ret + current.toString() + "\n"; current = current.getNext(); } return ret;} }
Background image of page 2
CS1316 Spring 2008 Practice Exam Final NAME: ___________________________________ PRISM ID:_______________________________ 3 Student.java StudentNode.java Write a new toString method in StudentList that contains no loops (while or for) and is recursive. (Hint: figure out what parameters the method needs to take in). public class StudentNode { private Student data; private StudentNode next; public StudentNode(Student data){this(data, null);} public StudentNode(Student data, StudentNode next) { this.data = data; this.next = next;} public Student getData() {return data;} public void setData(Student data) {this.data = data;} public StudentNode getNext() {return next;} public void setNext(StudentNode next) {this.next = next;} public String toString(){return data.toString();} } public class Student { private String name; private int grade; public Student(String name){this(name, -1);} public Student(String name, int grade){ this.name = name; this.grade = grade;} public String getName() {return name;} public void setName(String name) {this.name = name;} public int getGrade() {return grade;} public void setGrade(int grade) {this.grade = grade;} public String toString(){return "Name: " + name + " Grade: " + grade;} }
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
CS1316 Spring 2008 Practice Exam Final
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}