Category Archives: Computer Science

Rules Conventions for Variable Names

Employ the rules/conventions below. -1 point  for violating these standards

Variable Name Rules

  • must begin with a letter or $ (Unless you’re using a language like PHP it should be a letter)

Variable Name Conventions

  • descriptive  ( moo  is not a good variable name to represent the number of days )
  • uses camelcase (unless it’s a static variable etc..)
  • camelcase

^^image from https://khalilstemmler.com/blogs/camel-case-snake-case-pascal-case/

Jslider Extra Credit Jeroo Map

 
Description: Pick all of the flowers in the map below  using just 1 while loop
  • However, the loop does not need to end. Meaning after all the flowers are picked it’s ok for the Jeroo to continue to go through the loop and move around forever.
  • However, if you can do it with just 1 while loop  (you can get extra credit). See how Matt k did it
  • Map location: maps-student\mazes\mazes-2\jslider-map.jev
  • Extra Credit amounts :
    • Used 50 lines or fewer = Max credit
      • Tip : To try to get this few lines of code you will probably need to avoid a custom method for interior of lake.
    • Used  60 lines or fewer   = 3 points EC (that’s a lot)
    • Used  70 lines or fewer   = 2 points EC (that’s a lot)
    • Used 100 lines or fewer = 1 points EC
Current record for fewest lines : 32 lines of code
The Kukai Method
jslider-map-kukai

The Sam Smith Method
uber-map-sam-s

The Hamza Method
uber-map-hamza-solutio

Cody Smith Method

crop-image

Christian’s Bismarck  Solution

bismark

PlayList Java Project 2022-23

A Note on plagiarism

If you hand in work that you can not  explain, it is considered plagiarism.   Let’s not go there please. Remember- we follows Syracuse’s academic guidelines . If you use someone else’s work for a portion of the assignment, you will get a zero for the entire assignment. Additionally, if a person in the class gives you the code, that other person also gets a zero.

Project Overview:

A PlayList project will be based on 3 Classes

  • Artist
  • Song
  • PlayList

The Artist  class will store core information about an artist.

The Song  class will store basic information about a song.

The PlayList  class will manage a list of artists and their ratings.

Tester files:

  • Artist and Song tester file SongArtistTester.v0  – run this after you have completed the Song and Artist classes and before PlayList. This way, you’ll know that you have correctly done the the foundation classes before moving onto PlayList
  • PlayList tester File :  PlayListTesterv4
  • At the very bottom, are 2 challenging extra credit methods. If you decide to do either one, you must meet with me to go over the code you submitted.
  • Additional grading penalties for the following issues :

Penalties  :

  • does not compile : -5
  • instance variables not explicitly declared private  : -1
  • not following strict OOP conventions for initializing variables . Read more here.
  • lack of code reuse   (-1 per occurrence)
  • non descriptive variable name including loop index counters etc   : ( -1 per occurrence)
  • not using camelcase for variable names (-1)
  • int division -1
  • failure to name a method or class exactly as indicted (-1 per occurrence)  – which is a real thing . You will generally work in teams and be expected to use exact spelling/capitalization.

The 3 classes in Detail 

Artist

  • Constructor(s) :

    • public Artist(String name)
  • Instance Variables
    • private String artistName ;
    • private ArrayList<Song>  songs  ; – an array storing the songs of this artist
  • Methods
    • public void addSong(Song song)  .  Add the song to the arraylist.
    • public String toString()
    • public boolean equals(Artist other)  //returns  true  if artistName  is the same as other’s artistName 
    • public ArrayList<Song> getSongs()
    • public String getName()

Example of instantiating an Artist object and calling one of its methods:


Song

  • Constructor(s):
    • public Song( Artist _artist, String _name)
  • Instance Variables
    • private Artist artist
    • private String name
  • Methods
    • public String getName()
    • public Artist getArtist()
    • public boolean equals(Song other) //returns  true  if both the song objects have the same artist and name.
    • public String toString()

Example of instantiating a Song object :


Before you do the PlayList class, run the Artist/ Song tester (see top ^^)


PlayList

Read this to make sure you’re doing variables correctly.

 

  • Static Variable
    • public static final int MAX_NUMBER_SONGS = 12 ;

  • Instance Variables

    • private String listName  ; This is the ‘name’ of your playList
    • private ArrayList<Song> songs  ;
    • private ArrayList<Integer> stars   ;//how many stars each song has
    • Note: songs and stars are parallel ArrayLists
  • Constructor
    • public PlayList(String name)  : There should be only 1 constructor that takes a parameter representing the name of the playList. Read this 
  • Accessor Methods
    • public double averageRating()  // returns the average star rating for the  list
    • public ArrayList<Song> getSongs()  // returns the songs
    • 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 parameter artist
    • public ArrayList<Artist> getArtist(String songName)  // returns an ArrayList of all Artists associated with the String songName  (This could be multiple musicians. Cover songs etc..)
    • private int indexOf(Song someSong)  // code reuse, hint hint. ie This is the method that you should use elsewhere any time you need to find an index.
    • public String toString()  //returns an appropriate String representation of the PlayList  . Here is an example of the toString() value for a  PlayList I created.
    • public String getName()  // @returns the listName  of the PlayList
    • public ArrayList<Integer> getStars()  //returns the  stars  ArrayList

    Mutator Methods

    • public void swap(Song song1 ,  Song song2, )  // switches positions of these two (maintain parallelism!)
    • public boolean add(Song _song , int _stars)
      //adds data if number of song is less than MAX_NUMBER_SONGS
    • public void removeSong(Song song)  /removes all occurrences of  song  . There could be multiple instances of song. Maybe someone really likes a particular song and has it several times in one list. (Hint : Do not loop forwards)
    • 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 or equal to  cutOff

 


The 2  method’s below are extra credit. If you figure out how to do them, you must also meet with me after school about the methods. To receive the extra credit, be prepared for a mini-quiz on how they work . You will need to do some research to figure out how to do them.  Be prepared for me to ask about your solution and how changing certain parts of your code would affect your solution.

  • **  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
    • To do this , you should use a sorting algorithm. I suggest, Selection Sort (link). As this both intuitive and on the AP.
  • **  public PlayList shuffle()/ /this returns a new PlayList in which all of the songs have been reordered randomly

When you’re done this, begin exploring different sorting algorithms with this online sorting detective :

https://labs.penjee.com/sort-detective/

sort-detective

 

Prior version

How to Access 1 digit at at time of an int in Java

How to access each digit in an int in Java

 

Array Fun 3 [Resizing] Version 2

Version 1

ArrayFun3 - Please call your class that , exactly. Yes, you will lose a point for not following this simple specification.

  • use meaningful variable names
  • use  camelCase for variable and method names
  • use  i  as the variable name for for-i loops . Remember – programming is generally done in teams so your code should be  as expressive as possible. Use conventions to convey meaning, not cause confusion.
  • do not use i as the variable name in for-each  loops. Again, this would be misleading and imply you’re using a different type of loop.
  • do not import any external libraries . All solutions can be done by making use of variables and arrays and some basic math ; nothing more is needed.

 

int[]  removeZeroes(int [] nums)

Description: This method returns a version of nums  in which all occurrences of 0 , zero, are  removed

Method Call return value/output
noZeroes(  { 3 , 4, 0, 1} ) { 3, 4, 1}
noZeroes(  { 0, 5 , 0, 0, 9, 0, 1 , 11} ) { 5,9,1,11}

String[]  removeNulls(String[] strs)

Description: This method returns a version of  strsin which all null values are removed. Note: removeNullsv2

 


int[] digitsToArray(int n)

@precondition n > 9

Description: This method takes each digit in the integer n  and stores it in an array in reverse order, as shown below. First, make sure you understand the code here. , which btw, testers had to figure out , on the fly, in a previous AP A test.

digitstoarray


int[] removeLeadingZeroes(int[] nums)

@precondition nums.length >=2 

Description: This method returns an array in which all elements that meet the 2 criteria below are removed.

  • the element has a value of 0, zero
  • the element’s index is less than the index of the first element whose value is not zero.

 

remove_leading_zeros


 int[]  doubleUp( int  [] nums, int val)

@precondition nums.length >=2 

Description: This method returns a version of nums –with all occurrences of val   duplicated.

Method Call return value/output
doubleUp(  { 3 , 4, 0, 1}  , 4  ) { 3, 4, 0, 1}
doubleUp(  { 1, 3 , 1, 5 , 3, 3} ,  3 )    {1 , 3,  , 1, 5 , 3, 3, 3, }

 

 

 

Similar problems on codingbat

Pre4

Unit 2 Extra Credit

In addition to the required Unit 2 Creative Task, if you want some extra credit, you can do the following assignment:

Research how groups work in CMU ( see the docs)

Write a program that

  • includes two functions
  • minimum of 5 objects
  • A group comprised of at least 3 objects
  • One of the functions must change the properties of all things in the group.

You can write this in one of empty creative a tasks.

CMU Unit 2, creative task – mousepress mousedown

For this creative task,

This is a 10 Point assignment:

To get 9 points:

  • minimum of 2 shapes
  • minimum of 2 functions
    • 1 must be a mouse function (mousePress, mouseMove, mouseRelease)
    • the other function can be any function at all
    • one of the functions must change a property (fill, width etc) of the shape

To get the 10th point,

Research how groups work in CMU ( see the docs),

  • Use a  group comprised of at least 3 objects
  • 1of the functions must change the properties of all things in the group.