mportant: This is an individual assignment. Please do not collaborate. No late a
ID: 3811425 • Letter: M
Question
mportant: This is an individual assignment. Please do not collaborate. No late assi ment will be acc ted. sul in a Mou write every line ofyour code Using code written h ofthe academic int and will What This Assignment Is About: Arrays Classes Searching Use the following Coding Guidelines: Give identifiers semantic meaning and make them easy to read (examples numStudents, grossPay, etc). User upper case for constants. Use title case (first letter is upper case) for classes. Use lower case with uppercase word separators for all other identifiers (variables, methods, objects). Use tabs or spaces to indent code within blocks (code surrounded by braces. This includes classes methods, and code associated with ifs, switches and loops. Be consistent with the number of spaces or tabs that you use to indent. Use white space to make your program more readable. Part 1: Wri en Exercises 4 oints Note: The answers to the following questions should be typed in the block of comments in the Assignemn ava file. Please make sure they are commented out. Type them neatly and make them easy to read for the graders Exercisei: write statements to do the following: (4 pts) a) Create an array named "numbers" to hold 10 double values. b) Assign value 7.2 to the last element in the array. c) Display the sum of the first two clements, using System.out.println(...) d) Create another array named "numbers2" with initial values 5.7, 9.1, 7.21, and 8.4, using the array initializer. Your assignment is to create a class called Character in a file called CharacterListjava. (there is no main method in this class). A class CharacterList has the following instance variables (they must be declared usingExplanation / Answer
Part1:
Assingment6.java:
public class Assignment6 {
public static void main(String[] args) {
// double numbers[] = new double[10];
// numbers[9] = 7.2;
// System.out.println(numbers[0] + numbers[1]);
// double numbers2[] = {5.7, 9.1, 7.21, 8.4};
}
}
Part2:
CharacterList.java:
public class CharacterList {
private int count;
private char charArray[];
public CharacterList(int arraySize) {
this.count = 0;
charArray = new char[arraySize];
for(int i=0; i<count; i++)
charArray[i] = ' ';
}
private int indexOf(char searchingChar) {
int index = -1;
for(int i=0; i<count; i++) {
if(charArray[i] == searchingChar)
return i;
}
return index;
}
public boolean addCharacter(char charToAdd) {
if(indexOf(charToAdd) != -1)
return false;
if(count == charArray.length) {
doubleArrayCapacity();
}
charArray[count++] = charToAdd;
return true;
}
public boolean removeCharacter(char charToRemove) {
if(indexOf(charToRemove) == -1)
return false;
int i=0;
for(; i<count; i++) {
if(charToRemove == charArray[i])
break;
}
// now i points to the char to be removed
charArray[i] = charArray[count-1];
charArray[count-1] = ' ';
count--;
return true;
}
private void doubleArrayCapacity() {
char newArray[] = new char[charArray.length * 2];
int i=0;
// copy elements from old array to new
for(; i<count; i++) {
newArray[i] = charArray[i];
}
// copy space to rest of elements
for(; i<newArray.length; i++) {
newArray[i] = ' ';
}
// assign address of new array to old
charArray = newArray;
}
public char findLargest() {
if(count == 0)
return ' ';
// assuming first element to be largest
char largest = charArray[0];
for(int i=1; i<charArray.length; i++) {
if(charArray[i] > largest)
largest = charArray[i];
}
return largest;
}
public char findSmallest() {
if(count == 0)
return ' ';
// assuming first element to be smallest
char smallest = charArray[0];
for(int i=1; i<charArray.length; i++) {
if(charArray[i] < smallest)
smallest = charArray[i];
}
return smallest;
}
public int computeSumOfUnicode() {
if(count == 0)
return 0;
int sum = 0;
for(int i=1; i<charArray.length; i++) {
sum += (int)charArray[i];
}
return sum;
}
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("(");
char separator = '';
for(int i=0; i< count; i++) {
sb.append(separator + "'" + charArray[i] + "'");
separator = ',';
}
sb.append(") ");
return sb.toString();
}
}
Your instruction has said that there is some program Assignment6.java is available which has main method to use the code defined above in part2. Hence, i can not run the code in my system and provide you with output.. Please do run it on your system.. in case you get some errors, please tell in commnets.. i will try to help you..
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.