Author Archives: Mr. M
Compiled and Interpreted Languages
Alice- What as a String
You cannot place a number variable into the say() method
You must use the World function : what as a string
NEXT: asking user for input and saying it
Class Roster Array Version [Java] Assignment
Class Roster: This class will implement the functionality of all roster for school. It will , in essence, manage an Array of Student objects. Each roster will store a default of 10 Students
- “global” Instance Variables
- private Student myStudents : an array storing Student Objects
- private String className (a name to represent a given roster like “Ap CS”)
- public int period //during which period of the day does this roster meet
- a default constructor should initialize the Array to store 20 strings
a single parameter constructor that takes an Array <String>
Both constructors should initialize the instance variables - Methods
- Accessors
- private int indexOf(Student st) //@returns index of Student St or -1
- public boolean containsStudent(Student studentName )/ /@returns true if studentName is in roster .
- public boolean equals(ClassRoster other) //@ returns whether or not rosters are equal. Rosters are equal if identical students are in identical order in the lists
- Mutators:
- Accessors
1 |
boolean addStudent( Student st ) ;//adds the student to the Roster |
public void addStudent(String studentName, int age) ;// adds student name and age to end of roster
. Hint: Make user of the containsStudent() method
public boolean removeStudent(int ssnId ) // removes student from roster based on id. Make sure that you maintain the integrity of the parallel ArrayLists.
public boolean removeStudent(String name) //removes student based on name. Make sure that you maintain the integrity of the parallel ArrayLists.
public boolean removeStudent( Student st) //removes student st. Make sure that you maintain the integrity of the parallel ArrayLists.
Student Runner Part 2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
public class StudentsRunner2{ public static void main(String[] args){ //insert the line of code needed to create the Student object aStudent; String teacher; for (int i =0 ;i < 5; i++) { teacher= inputString("Enter a teacher"); aStudent.addTeacher(teacher); } } } |
1) Construct the Java statement to create the Student object called aStudent in the
StudentsRunner
2)
Back to the Student Class
Students Runner Class
1 2 3 4 5 6 7 8 9 10 |
public class StudentsRunner{ public static void main(String[] args){ Student dom; } } |
1) Construct the Java statement to create the Student object called Dom in the
StudentsRunner
Back to the Student Class
Student Object (OOP Java)
Student Class
A school district decided to write a program to help keep track of various parts of the school.
It has been decided to store relevant information about Students in a Student class. Each Student will have an array of Strings to store the names of his or her teachers.
private instance variables
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
public class Student { public String name; private int age private double gpa; private //insert array declaration; public Student(){} // default constructor boolean setTeacher (String name, int index){ //missing code } public void setGpa(double to){ //missing code } public double getGPA() // returns gpa } |
Part 1
* Construct the Java statement to declare the array called
teachersNames to store the String names of the student’s teachers
Part 2
- write a default constructor
- construct a method called
addTeacher( String name) that adds
String name to the
teachersNames array
- to do this let’s create a private method private String[] addStr( String[] input, String val) that adds value to the end of the array and returns it. [redact]Solution[/redact]
- construct hasTeacher(String name) that returns true if name is in the teachersName array
- construct a method removeTeacher(String name) that removes the String name from the teachersNames array.
- construct a method the line of code needed in the setGpa(double to) method of the Student Class.
- construct a method boolean setTeacher (String name, int index) . This method should return true if index is valid in the array and false if index >= teachersName.length
Playlist Project [Array]
A PlayList project will be based on Three Classes
The Artist class will store core information about an artist
The Song class will store basic information about a music song
The PlayList class will manage lists of songs and artists
Artist
- Constructor(s) :
public Artist( String name) { //missing code }
- Instance Variables (“global” variables)
1 2 |
private String artistName ; private Song[] songs ; - an array storing the songs of this artist |
Methods
1 2 3 4 5 |
private int indexOf(String songName) // returns index position of parameter in the array public void addSong(Song song) public void removeSong(Song song) public void removeSongByName(String songName) public Song[] getSongs() //returns songs array public String getName() // returns artistName |
Song
Constructor(s):
1 2 3 |
public Song(String songName, Artist artist){ //missing code } |
Instance Variables
1 2 3 |
private Artist artist ; public String songName ; public long date //(date added to list) |
example of how to get current milliseconds as a long
Method
1 |
public Artist getArtist() |
PlayList
- Instance Variables
- private String listName : This is the ‘name’ of your playList
- private Song[] songs;
- private int[] stars ; //how many stars each song has between 0 and 5 inclusive.
- Note: songs and stars are parallel Arrays
- Constructor
- public PlayList(String name) : There should be only 1 constructor that takes a parameter representing the name of the playList
- Accessor Methods
- public double averageRating() // returns the average star rating for the list
- public double averageRating(Artist artist) // returns the mean star rating associated with artist
- public Song[] getSongs(Artist artist) // returns an array populated by the songs of the parameter artist
- public Song[] getSongs() // returns all the songs in the list
Mutator Methods
- public void swap(Song song1 , Song song2 ) // switches positions of these two (maintain parallelism!)
- public void add(Song song , int stars)
-
public void removeSong(Song song, String artist ) //removes song associated with artist
- Note: Be careful here. You are allowed to add the same song and artist. Duplicates are allowed.
- public void removeArtist(Artist artist ) //removes all elements associated with artist
- public void removeLowStars(int cutOff) //removes all elements associated with a star rating less than cutOff
- ** public PlayList sortByDate() //this returns a rearranged PlayList based on each Song’s date
- ** public PlayList sortByRating() //this returns a rearranged playlist so that the 5 starred elements are the first group in the list, 4 stars second …1 stars, last
- ** public PlayList shuffle() //this returns a new PlayList in which all of the songs have been reordered randomly.
Leadership Qualities
Read the following articles and answer handout questions
Steve Jobs article : http://www.forbes.com/sites/jackzenger/2013/08/22/the-big-lesson-about-leadership-from-steve-jobs/
Mark Zuckerberg #1 http://www.inc.com/ekaterina-walter/as-zuckerberg-turns-30-leadership-lessons.html
Mark Zuckerberg #2 [article taken down 🙂 ]
Array Fun 2
Java Assignment
Create a Class called
ArrayFun2 that has the following methods Write the body for the methods described below.
1 |
int[] digitsToArray(int num) |
Description: This method creates an array that is filled with all of the digits of
nums
.
Method Call | return value/output |
digitsToArray( 523 ) | {3, 2, 5} |
digitsToArray(1267 ) | { 7 , 6 , 2, 1 } |
public boolean allFactorsOfSum(int[] nums)
Description: This method returns true if each element of
nums
is a factor of the sum ofnums
.
Method Call | return value/output |
allFactorsOfSum( {6,1,2,3} ) | true (because all of the elements are factors of the sum which is12) |
allFactorsOfSum( 1,4,7) | false (because 7 is not a factor of the sum of this array which is 12) |
public String[] doubleArr(String[] strs)
Description: This method returns a new version of
strs
with each element appearing twice.
Method Call | return value/output |
doubleArr( {“a”,”b”,”c”} ) | {“a”,”a”,”b”,”b”, “c” , “c”} |
doubleArr( {“math”,”ware”,”house”,”.com” }) | {“math”,”math”,”ware”,”ware”,”house”,”house”,”.com”, “.com”} |
public boolean isThere(int[] nums, int num)
Description: This method returns true if
num
is an elementnums
.
Method Call | return value/output |
isThere( {6,1,2,3}, 6) | true |
isThere( {6,1,2,3}, 5 ) | false |
public int indexOf(int[] nums, int num)
Description: This method returns the index value of the first appearance of
num
or -1 ifnum
is not an element ofnums
.
Method Call | return value/output |
indexOf( {6,4 ,7,3, 4 }, 4) | 1 |
indexOf( {6,4 7 ,3,2,7}, 7) | 2 |
indexOf( {6,4 ,2,3}, 22) | -1 |
public int lastIndexOf(int[] nums, int num)
Description: This method returns the index value of the last appearance of
num
or -1 ifnum
is not an element ofnums
.
Method Call | return value/output |
lastIndexOf( {6, 4 ,7 ,3, 11, ,4}, 4) | 5 |
lastIndexOf( {7, 6,4 , 7 ,3}, 7) | 3 |
lastIndexOf( {6,4 ,2,3}, 22) | -1 |
public boolean isIncreasing(double[] nums)
Description: This method returns true if each element in nums is greater than the element to its left.
Method Call | return value/output |
isIncreasing( {1,2,3,4 }) | true |
isIncreasing( {1,0 ,3,4 }) | false |
isIncreasing( {1,1, 2,3,4 }) | false |
public int largestSpan(int[] nums)
Description: This method returns the largest span of consecutive increasing numbers
Method Call | return value/output |
largestSpan( {4, 3, 1, 2, 3 , 1} ) | 3 |
largestSpan( {4 , 3, 1 , 12 , 31 , 44 , 52 , 1} ) | 5 |
largestSpan( {9, 7, 8 , 12,4, 3, 0, 4 , 1) | 3 |
largestSpan( {1, 0, 4, 5 , 3, 2, 8 , 9, 10, 12, 4, 3, 1 }) | 5 |
** int[] invert(int[] nums)
Description: This method ‘inverts’ an array by spliting the array in halves and ‘inverting’ each half. See sample calls to understand.
Method Call | return value/output |
invert( {5 , 21, 5, 13,4 }) | {4, 13, 5, 21, 5 } |
invert( { 1, 2, 3, 4, 5,6 }) | { 6, 5, 4, 3, 2, 1} |
** int[] shiftByN(int[] nums, int delta)
Description: This method returns the array with all digits shifted by
delta
. Any digits that are circulated off the end of the array should be returned to the other side. Note: delta could be positive or negative. Please keep in mind that Math.abs(delta) > nums.length could be possible 😉
Method Call | return value/output |
shiftByN( {5 , 21, 13,4 } 1) | { 4 , 5 , 21, 13 } |
shiftByN( {5 , 21, 13, 4 , 11} 2) | { 4 , 11 , 5 , 21, 13} |
shiftByN( {5 , 21, 13, 4 } -1) | { 21, 13, 4 , 5} |
shiftByN( {5 , 21, 13, 4 ,7 } -2) | { 13, 4 ,7 , 5 , 21} |
1 < base < 11
@precondition
num1.length = 5 and num2.length = 5
@postcondition: the returned array has 5 elements representing the sum or an ArithmeticException is thrown
Description: This method attempts to replicate addition. Consider both num1 and num2 represent the five digits of a number. Each element in the array stores one of the digits. For instance, the number 143 would be represented as {0,0,1,4,3 }. Numbers can be in any base between 2 and 10 inclusive , and the number 101102 in binary would appear in an arrays as : {1, 0, 1 , 1, 0 } . Let’s assume the numbers are positive.You should return an array representing the digits of the sum of num1 and num2. If the number of digits in the sum exceeds the maximum number of digits (5) , you should throw an arithmetic exception as shown in the code below:
12 if(overFlowAdditionOccurred)throw new ArithmeticException("Addition Overflow Error");
Note: You may not use any kind of helper or utility methods that are built into pre-defined Java classes for converting numbers between bases. Only use techniques taught in this class.
Method Call | return value/output |
add( {0,0,0 ,4,2},{0,0,0,5,1}, 10) | {0, 0, 0, 9, 3} ie (42 + 51 = 93) |
add( {0, 0, 0 ,7,2},{0,0,0,5,1}, 10) | {0, 0 , 1 , 2, 3} ie (72 + 51 = 123) |
add( {0, 0 , 0 , 1 , 1}, { 0 , 0 , 0 , 1 }, 2) | {0, 0 , 1 , 0 , 0} ie ( 112 + 12= 1002 ) |