Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I'm trying to get my Weight value in my array for my Persons class, but when I try to call the value to add them

I'm trying to get my Weight value in my array for my Persons class, but when I try to call the value to add them up to a total weight to determine the value needed to clear a threshold, The method getWeight() is undefined for the type ArrayList. Is there any way I can get the value of my int weight value so that I can add them together?

import java.util.ArrayList; import java.util.InputMismatchException; import java.util.List; import java.util.Random; import java.util.Scanner; class OregonTrailGame { int money; ArrayList riders; ArrayList wagon; ArrayList item; Scanner scanner; Items[] objects_for_use = {Objects.item1, Objects.item2, Objects.item3,Objects.item4, Objects.item5, Objects.item6, Objects.item7 }; // Add more items as needed Persons[] riders_in_game = {Objects.rider1, Objects.rider2, Objects.rider3, Objects.rider4}; Wagon[] wagons_in_game = {Objects.wagon1}; Bandit [] enemy_Numbers = {Objects.bandit1, Objects.bandit2}; static int points; OregonTrailGame() { this.riders = new ArrayList(); this.money = new Random().nextInt(501) + 1000; // Money call random between 1000 and 1500 this.scanner = new Scanner(System.in); this.item = new ArrayList(); this.wagon = new ArrayList(); } void startGame() { initializeRiders(); assignRandomItems(); System.out.println("Great! You have $" + money + " and the following riders:"); displayRiders(); endGame(); } void initializeRiders() { System.out.println("Welcome to the Oregon Trail Game!"); System.out.println("Enter the number of riders (3 or 4): "); int numRiders; do { while (!scanner.hasNextInt()) { System.out.println("Invalid input. Enter a number (3 or 4): "); scanner.next(); // Consume the invalid input } numRiders = scanner.nextInt(); scanner.nextLine(); // Consume newline } while (numRiders != 3 && numRiders != 4); for (int i = 0; i < numRiders; i++) { System.out.println("Enter name for Rider " + (i + 1) + ": "); String riderName = scanner.nextLine(); riders_in_game[i].setName(riderName);; riders.add(riders_in_game[i]); } } void assignRandomItems() { Random random = new Random(); int randomItem = 1; for (Persons rider : riders) { Items randomItemName; do { randomItemName = objects_for_use[random.nextInt(objects_for_use.length)]; } while (!randomItemName.equals("special items")); rider.setItemslot(randomItem); } } void displayRiders() { for (Persons rider : riders) { Persons riders; riders.getItemslot(); if (rider.getItemslot() == 1) { System.out.print(((Persons) riders).getName() + " with "); for (Items item : item) { System.out.print(item.getName() + " "); } System.out.println(); } } } private void endGame() { calculateScore(); scanner.close(); } private void calculateScore() { int score = riders.stream().mapToInt(r -> r.getInventory().size()).sum() + money + points; if (score > 0) { System.out.println("Congratulations! You won with a score of " + score); } else { System.out.println("Sorry, you lost. Better luck next time!"); } } //cindy void handleCanyonScenario() { int equippedItem1 = riders.getItemslot(); int equippedItem2 = riders.getItemslot(); Random random = new Random(); boolean isFirstRiderFalling = random.nextBoolean(); ArrayList fallingRider; ArrayList survivingRider; if (isFirstRiderFalling) { fallingRider = riders; survivingRider = riders; } else { fallingRider = riders; survivingRider = riders; } // Logic for canyon scenario based on equipped items if (("pickaxe".equals(equippedItem1) && "rope".equals(equippedItem2)) || ("rope".equals(equippedItem1) && "pickaxe".equals(equippedItem2))) { points = points + 100; // points for using makeshift grappling hook System.out.println("Both riders have escaped with created grappling hook!"); } else if ("grappling_hook".equals(equippedItem1) || "grappling_hook".equals(equippedItem2)) { // store bought grappling hook - both riders escape points = points + 200; // double points for using the grappling hook System.out.println("Both riders escaped using the grappling hook but broke it in the process."); } else { // 1 rider survives // Determine which rider will escape based on your conditions if (riders.gethunger() > 20 && riders.gethp() > 50) { System.out.println(riders.getname() + " escaped!"); points = points + 50; // points for escaping } else { System.out.println(riders.getname() + " escaped. " + riders.getname() + " didn't make it."); points = points + 50; // points for escaping } } } public void River_Trial() { //resolve rider vs persons int weight = riders.getWeight() + wagon1.getTotalWeight(); boolean b = false; { do { try { System.out.print("Your riders have come across a river that has flooded, you can either:" + " wait for the River to subside at the local Inn (1)" + " try and trek the River with the riders in the wagon (2)"); Scanner n = new Scanner(System.in); int bn = n.nextInt(); if (bn == 1) { System.out.println("You decided to rest at the Inn and spent $400 for one night"); System.out.println("You now have $ " + money); money = money - 400; } else if (bn == 2) { System.out.println("You try to trek the river with the current riders on the wagon"); } } catch (InputMismatchException e) { System.out.println("Please enter a number between 1 and 2"); } } while (!b); } } } public class ObjectsScore { public static void main(String[] args) { new OregonTrailGame().startGame(); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Handbook Of Relational Database Design

Authors: Candace C. Fleming, Barbara Von Halle

1st Edition

0201114348, 978-0201114348

More Books

Students also viewed these Databases questions