Trio - Akhil N, Tristan C, Adi K
public static int hailstoneLength(int n) {
int t = 1;
while(n>1){
if(n%2==0){
n = n/2;
}
else{
n = n*3 + 1;
}
t++;
}
return(t);
}
hailstoneLength
that takes an integer n
as input and returns the length of the hailstone sequence starting at n
.
t
for each step.
n
is even, it is divided by 2, otherwise it is multiplied by 3 and added to 1.
n
reaches 1.
public static boolean isLongSeq(int n){
return hailstoneLength(n) > n;
}
isLongSeq
that takes an integer n
as input and returns true if the length of the hailstone sequence starting at n
is greater than n
.
hailstoneLength
method with n
as input and compares the result to n
.
public static double propLong(int n){
int count = 1
for(int i=1; i<=n; i++){
if(isLongSeq(i)){
count++;
}
return (double) count/n;
}
}
propLong
that takes an integer n
as input and returns the proportion of integers from 1 to n
for which the hailstone sequence length is greater than the starting integer.
count
to 1 and iterates through the integers from 1 to n
.
isLongSeq
method and increments count
if it returns true.
count/n
as a double.
import java.lang.Math;
public class Gamespinner{
public int sectors;
public int lastSpin;
public int run;
public Gamespinner(int n){
sectors = n;
lastSptn = 0;
run = 0;
}
public int currentRun(){
return run;
}
public int spin(){
int newSpin = Math.random()*sectors + 1;
if (newSpin == lastSpin){
run++;
}
else{
run = 1;
lastSpin = newSpin;
}
return newSpin;
}
}
Gamespinner
that simulates a game spinner with a specified number of sectors.
sectors
(number of sectors), lastSpin
(the result of the previous spin), and run
(the length of the current run of consecutive identical spins).
currentRun
, which returns the current run length, and spin
, which simulates a spin of the spinner by generating a random integer between 1 and the number of sectors.
run
variable is incremented. Otherwise, the run
variable is reset to 1 and the lastSpin
variable is updated with the new spin result.
public void addReview(ProductReview prodReview){
reviewList.add(prodReview);
String temp = prodReview.getName();
for(int i = 0; i<productList.size(); i++){
if !(temp.equals(productList.get(i))){
productList.add(temp);
}
}
}
addReview
that takes a ProductReview
object as input and adds it to a list of reviews called reviewList
.
productList
.
productList
, it is added to the end of the list.
public int getNumGoodReviews(String prodName){
int count = 0;
for (int i = 0; i < reviewList.size(); i++){
if prodName.equals(reviewList.get(i).getReview().indexOf("best")){
count++;
}
}
return count;
}
getNumGoodReviews
that takes a product name as input and returns the number of reviews in the reviewList
where the review text contains the word "best".
count
to 0 and iterates through the reviewList
.
indexOf
method.
count
variable is incremented.
count
variable.
public Theater (int seats PerRow, int tierlRows, int tier 2 Rows){
theaterSeats = new Seat [tierlRows+tier2Rows] [seats PerRow];
for (int r = 0; r<theaterSeats.length; i++){
for(int c = 0; c<seats PerRow; c++){
if (r<tier1 Rows){
theater Seats [r] [c]=new Seat (true,1);
}
else{
theaterSeats [r] [c]=new Seat (true, 2);
}
}
}
}
public boolean reassignSeat(int fromRow, int fromCol, int toRow, int toCol){
if(!theaterSeats[toRow][toCol].isAvailable()){ return false }
if(theaterSeats[toRow][toCol].getTier() >= theaterSeats[fromRow][fromCol].getTier()){
theaterSeats[toRow][toCol].setAvailability(false);
theaterSeats[fromRow][fromCol].setAvailability(true);
}
else{
return false;
}
}
unkown • 6/16/2025, 12:07:10 PM