Author Archives: Mr. M

Student Class

This class will be used in our Class Roster assignment .

Here is a client  test file . This tester file should not be   in the same class/file as Student . Instead. just put the tester into BlueJ into class called StudentRunner , within  the same BlueJ project,  and run the client file.

Instance variables

  • private String name
  • private int age
  • private int socialSecurityNum  (  How can we guarantee unique social security  numbers? )

Constructors

  • default
    • public Student()   .
  • public Student( String _name)
  • public Student (String _name,  int _age)

Accessors

  • public int getSSN()
  • public int getAge()
  • public String getName()
  • public boolean equals(Student other)
  • public String toString()

Mutators

  • public void setAge( int  _age)
  • public void setName( String to)

Alice Projects (if-else and loops)

 

  • Packets #12 -14
  • 1)Kick Ball Create a world with your choice of a ball from the Sports collection and your choice of person from the People collection. Position the ball in front of one of the person’s feet. When you play the world, it should ask the user how far the person should kick the ball. Then the person object should move one of its legs in a kicking motion, and appear to kick the ball. The ball should move the distance specified by the user.
  • 2) Miles Per Gallon: Create a world in which the user is asked for the number of gallons of fuel his or her car can hold, and the number of miles that he or she can drive on a full tank. Use variables to hold these values. You should also have a variable to hold the car’s miles-per-gallon (MPG). Calculate the MPG with the following formula

    The world should have a person and a car. In the initial setup the car should approach the camera and the car should drive into view and stop in front of the camera. The person should then say a message indicating the car’s MPG, as calculated from the data entered by the user. Then, the car should drive away, to a position of the world
  • 3) Fahrenheit to Centigrade : The following formula is used to convert Fahrenheit temperature to Centigrade temperature:
      C = (5/9) * (F − 32)

    In the formula, C is the Centigrade temperature and F is the Fahrenheit temperature. Create a world in which the user is asked to enter a Fahrenheit temperature. Use a math expression to convert the temperature to Centigrade and store the converted temperature in a variable. Use a character from the People collection to say the centigrade temperature.

  • 4) Circle’s Area and Circumference: Create a world with your choice of person from the People collection. When you play the world, it should ask the user how for the radius of a circle. (This value should be stored in a variable ). The person should then say the area and the circumference of the circle.(circumference formula , area of a circle) (For an extra point do the same thing with a “Sphere”. Ask the user for the radius and calculate the .
  • ** Project 5 )For those shooting for an A. Look up the formulas for the volume and surface area of a cylinder . Ask the user for the height of the cylinder and the radius, which should be stored in variables. Then calculate the surface area and volume of the cylinder . See if there is a cylinder shape in the gallery to use as a visual aid.

Part II

  • Complete Packets #15, 16
  • Exercise 1 GumdropFish Modification: Modify the GumdropFish world that you created in Tutorial 4-2 so that the fish eats both of the gumdrops. First it should eat the gumdrop that is closest (as it currently does) , and then it should move to the other gumdrop and eat it. Both gumdrops should have their magical effect on the fish: the red gumdrop should make the fish twice its current size, and the yellow gumdrop should make the fish half its current size.
  • Exercise 2 FanLoop Modification: Modify the FanLoop world on the Student CD so it asks the user for the number of times the fan blades should rotate. Use this value in the Loop statement to control the number of times the roll method is called.
  • Exercise 3 Sea Plane Loop-the-Loop : Create a world with a sea plane (from the Vehicles collection).Create an infinite loop that causes the sea plane to do a circular loop the loop. Adjust the style and duration editing tags to make the animation fast, and as smooth as possible.
  • Exercise 4 Centigrade to Fahrenheit Modification: Modify the Fahrenheit to centigrade project as follows. At the start, ask the user how many temperatures they want to convert. Then use a loop to do that many conversions
  • Exercise 5 Miles Per Gallon Modification: Modify the miles per gallon project in the same way as the prior exercise. Ask the user how many miles per gallons they want to convert, and then use a loop to repeat the miles per gallon calculations.

ArrayList 1 Excercises

BlueJ users. Read this about how to use ArrayLists as parameters. Or consider using Eclipse if this is too much of a hassle.

ArrayList<Integer> factors(int num)

Description: This method returns an ArrayList populated with the factors of num

Method Call return value/output
factors(5) {1,5}
factors(10) {1,2,5,10}

int sum(ArrayList<Integer> nums)

Description: This method returns the sum of all elements in the list

Method Call return value/output
sum({1,5}) 6
sum( { 3,4, 2 }  ) 9

void printReverse(ArrayList<String> strs)

Description: This method prints all elements in reverse order

Method Call return value/output
printReverse({“a”,”b”}) “b”
“a”
printReverse( “xu”, “t”) “t”
“xu”

double mean(ArrayList<Integer> nums)

Description: This method returns the arithmetic mean of all elements in the list

Method Call return value/output
mean({1,5}) 3
mean( {3,4, 2 } ) 4.5

void printEvens(ArrayList<Integer> nums)

Description: This method prints out all even numbers

Method Call return value/output
printEvens({1,5 , 6}) 6
printEvens( {3,4, 2 }  ) 4 , 2

ArrayList<Double> everyOtherPi(ArrayList<Double> nums )

Description: This method returns a version of the input ArrayList with every other value changed to Math.PI

Do NOT make a new ArrayList in this method

Method Call return value/output
everyOtherPi ( {1,8,5,0, 7} ) {1, 3.1415 , 5 , 3.1415 , 7 , 3.1415 }
everyOtherPi({0,4,3,5} ) {0, 3.1415 ,  3 , 3.1415 }

boolean isIdentical(ArrayList<String> strs1 , ArrayList<String> strs2)

Description: This method returns true if each element in the two ArrayLists is the same

Method Call return value/output
isIdentical({“ab”, “a”} , { “x”, “a”} ) false
isIdentical({“ab”, “a”} , { “ab”, “a”} ) true
isIdentical({ “xy”} , { “XY”} ) false

int largest(ArrayList<Integer> nums)

Description: This method returns the largest element in nums

Method Call return value/output
largest({1,5 , 6}) 6
largest ({3,4, 2 }  ) 4

ArrayList<Integer>removeVal(ArrayList<Integer> nums, int val)

Description:This method returns an ArrayList with all instances of val removed

Do NOT make a new ArrayList in this method

Method Call return value/output
 removeVal({1,5 , 6}, 5 ) {1, 6 }
removeVal  ( {3,3,3,4, 2} , 3 ) {4,2}

 

 

TO do :

PrimesFactory

2 D Arrays in Java Assignments

Add the following Methods to a class called ArrayFun2D

public void print(int[][] arr)

Description: This method prints each element in the array. This will be a useful method when you are debugging your code and want to test it out to see if it works.

Method Call return value
print({{1,2} , {3,4}, { 5, 6 } } ) 1   2
3  4
5   6

public int sum(int[][] arr)

Description: This method returns the sum of all elements in the array

Method Call return value
sum({{1,2} , {3,4}, { 5, 6 } } ) 21 ( ie 1+2+3+4+5+6)

public String concat(String[][] strs)

Description: This method returns the concatenation of all the elements in the Array

Method Call return value
concat({{“a”,”xc” }, {“t”, “zoo”}, { “jjkj” } } ) “axctzzoojjkj”

public double mean( int[][] arr)

Description: This method returns the arithmetic mean of the array. Make sure you double check that you are correctly returning results that include decimal values .

Method Call return value
mean({{2,8} , {4 , 7} } )  5.25 ie (2+8+4+7)/4

public void printRow(int[][] arr, int row)

Description: This method prints all elements in rowThis can be (and should be) done with a single loop.

Method Call return value
printRow({{1,2} , {3,4}, { 5, 6 } } , 1 ) 3, 4
printRow( { 5 , 6} , {7, 12, 13 , 5 }, { 5, 6 ,0} } , 2 ) 5 , 6, 0

public void printCol(int[][] arr, int col)

Description: This method prints all elements in col

Method Call return value
printCol({{1,2} , {3, 4}, { 5, 6 } } , 1 ) 2
4
6
printCol( { 5 , 6} , {7, 12, 13 , 5 }, { 12, 6 ,0} } , 0 ) 5
7
12
printCol(    {12, 13 , 5 }, { 6, 2, 13 ,11}  , {1,2 } } , 2 ) 5
13

public int maxVal_inRow(int[][] arr, int row)

Description: This method returns the value of the largest number in the given row

Method Call return value
maxVal_inRow({{1,2} , {3,4}, { 5, 6 } } , 1 ) 4
maxVal_inRow ({   { 5 , 6} , {7, 12, 13 , 5 }, { 5, 6 ,0} } , 2 ) 6

public int maxVal_inCol(int[][] arr, int col)

Description: This method returns the value of the largest number in the given col

Method Call return value
maxVal_inCol({{1,2} , {3,4}, { 5, 6 } } , 0 ) 5
maxVal_inCol({{1,-2} , {3,-4}, { 5, -5 } } , 1 ) -2

public int[][] changeValues(int[][] table, int val)

Description:This method returns the array with all elements set toval

changeValues(table, 5)

Original Array

1 2
3 4
5 6
Returned Array

5 5
5 5
5 5

public void print_colMajor(int[][] arr)

Description: This method prints each element in the array – using column major order

Method Call return value
print({{1,2} , {3,4}, { 5, 6 } } ) 1   3   5
2  4    6

 

public int[][] removeVals(int[][] table, int val)

Description: This method returns a version of the 2-d array where val has been removed from all elements

removeValues(table, 5)

Original Array

{1 , 5}
{3, 4}
{5, 6}
Returned Array

{1}
{3, 4}
{6}

 

int[] twoD_to1D(int[][] arr)

Description: This method takes an input array that is in 2 dimensions  and  creates a 1 d array . Assume that the array is rectangular (not jagged).

Method Call return value
twoD_to1D ( {{1,2} , {3,4}, { 5, 6 } } ) { 1 , 2 ,  3 ,  4 , 5 ,  6 }

Matrices

int[][] add (int[][] matr1, int[][] matr2)

Description: This method returns the sum of matr1 and matr2

how to add matrices


 

** int[][] multiply(int[][] matr1, int[][] matr2)

Description: This method returns the product of matr1 and matr2

how to multiply matrices

**public int[][] invertValues(int[][] matr)

Description: This method returns the paramater with its values ‘inverted’

Original Array

1 2
3 4
5 6
Returned Array

6 5
4 3
2 1

Two Dimensinal Arrays in Java

Two Dimensional Arrays in Java

 

2-D array Demo Code

2 D Array Assignments

Array 3 [ v1]

Warming up

public void  printFactors( int n )

Description: This method prints all factors of n

Method Call return value/output
printFactors( 5) 1

5

printFactors( 6) 1

2

3

6

printFactors( 9 ) 1
3
9
printFactors( 15) 1
3
5
15

 

public int[] getFactors( int n )

Description: This method returns an array populated with the factors on n

Method Call return value/output
getFactors( 5) {1,5}
getFactors( 6) {1,2,3,6}
getFactors( 9 ) {1,3, 9}
getFactors( 15) {1,3,5}

 

 

public int[]  removeZeros(int [] nums)

Description: This method returns a version of the input –with all occurrences of 0 , zero, removed

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

 

 

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

Description: This method retuns a version of the input –with all occurances 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 codinbat

Pre4

Mazes 2

Mazes #2

Shunya Map 1

shunya_1


Shunya 4

shunya_4

 

 

Struhl Map:

  • Start   20, 18
  • go around the interior, get the flower, pick it up.
  • 1 loop only

struhl_map

 

 

Heeyeon’s Map:

  • heeyeons_maze.jev
  • start Jeroo at (23,1) and pick up the flower  at that location
  • go to (1,23)
  • you may NOT pick a flower or visit a cell that has a flower (See animation at bottom)

Picture at start:

heeyons_maze1

One Solution:heeyeons_maze

 

Brett’s Maze 2

Description: Start at 0,0, pick all the flowers then use them to toss() your way through the nets.   bretts-maze2
 bretts-maze2

 

George’s Map  : Start at ( 10,0) , facing SOUTH,  Use 1 loop and pick the flowergeorges-map

 Paul’s Diagonal Map
pauls-diagonal

 

 Matt’s All Flower Map
Map name :   matt_water_flower_map.jev
Directions: pick up all the flowers. Use 1 while loop. You can start and end wherever you want. Just make sure that the loop eventually terminates.
One strategy:
 matts-all-flowers

A Different strategy

matt_water_flower_map


Jar jar’s Haven:

jarjarshaven

 

Bismark’s Map

Directions: Pick all the flowers. Start anywhere ; end anywhere. Don’t die, Make sure the loop ends.

bismarks-map

 

Diagonal Inwards
Fill up the Map
  • * Use a blank map and have 2 Jeroos fill up every spot with a flower.
  • Use a single loop (not an infinite one either)

diag-inwards-133percent

Sota
sota

EXTRA CREDIT:

Mazes 1

Objective: To use while loops to get the Jeroo to pass through each maze and get the flower. It is possible to write a solution to both of these mazes using a single while loop–albeit this loop will have several( about 5 or 6) if tests! It is important for this problem that you do NOT simply hand code this. To get an A on both of these assignments you must use 1 single while loop per maze.
Download All Island Files

 island3-A 

islands-3-a-opt


Shunya 3

shunya_3

 


Aliya’s Map

aliya



 Mazes 2 (start in top left, 1 loop to pick flower)

mazes-1-assign-mazes2

 


Shunya 2

shunya_2

 


Yuna’s Original Maze

 

yunas_original_maze


 

 

 island-3-D (start in the top left, and get to the bottom right)

island3-d

AlanasMessage_MAP.jev

alanasmessage_sped_up


4) ZigZag   : Use 1 while loop (not infinite), follow the flowers, picking them up and use those flowers traverse over the nets. Notice where the Jeroo ends (ie your while loop condition)

zigazg-opt

5)  Spell Z (Blank Map)

  1. start at 0 , 0 with 70 flowers.
  2. Use a single while loop to spell a letter z

spell-z-jeroo-with-loop_assignment-complete


 

Last and most definitely not least:

island3-C ** A+ assignment.

This is the deceptively hard and should be done last and only if you want that A+


 

 

Done all of the maps? Start on the next set here