Question

Part I: Create a program class named TestArrays This class should have a main() method that...

Part I:

  1. Create a program class named TestArrays This class should have a main() method that behaves as follows:
  2. Create an integer array of size 10.
  3. Using a loop, fill the elements with increments of 5, starting with 5 in the first element.
  4. Using the array elements, sum the second, fifth and seventh elements.
  5. Display the sum with a label.
  6. Change the fourth element to 86.
  7. Subtract 9 from the ninth element.
  8. Using a loop, display the elements of the array.
  9. Compile and execute the code to ensure correct results.
  10. Don’t forget to include descriptive and detailed comments in your code!!!

Part II

  1. Write a toString() method for the Vehicle class that would display the attributes of the class.
  2. Create an array of 7 elements that will hold Vehicle references.
  3. Using a loop, assign the first 4 elements to Vehicle objects using the null constructor.
  4. Instantiate a Vehicle object with a base price of $37,000 and additional features of $400.
  5. Assign the object to the 5th element of the array.
  6. Assign the 6th element of the array to a Vehicle object with a base price of $24,000 and additional features of $5500.
  7. Assign the 7th element of the array to a Vehicle object with a base price of $53,000 and additional features of $9000.
  8. Calculate the total price for the 5th, 6th and 7th vehicles in the array.
  9. Using an array, display the attributes of the objects in the array calling the toString().
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Part 1

public class Source {
    public static void main(String[] args) {
        int[] arr = new int[10];

        for (int i = 0; i < 10; i++) {
            arr[i] = (i + 1) * 5;
        }

        int sum = arr[1] + arr[4] + arr[6];
        System.out.println("Sum of second, fifth and seventh element : " + sum);
        arr[3] = 86;
        arr[8] -= 9;

        for(int x : arr){
            System.out.print(x + " ");
        }
    }
}

Output

Sum of second, fifth and seventh element : 70
5 10 15 86 25 30 35 40 36 50 
Process finished with exit code 0

Part 2

class Vehicle {
    int basePrice, additionalFeatures;

    Vehicle() {

    }

    Vehicle(int base, int add) {
        basePrice = base;
        additionalFeatures = add;
    }

    public String toString() {
        return "Base price : " + basePrice + ", " + "Additional Features : " + additionalFeatures;
    }
}


public class Source {
    public static void main(String[] args) {
        Vehicle[] v = new Vehicle[7];

        for (int i = 0; i < 4; i++) {
            v[i] = new Vehicle();
        }

        Vehicle v5 = new Vehicle(37000, 400);
        v[4] = v5;

        v[5] = new Vehicle(24000, 5500);
        v[6] = new Vehicle(53000, 9000);

        int price = 0;
        for (int i = 4; i < 7; i++) {
            price += (v[i].basePrice + v[i].additionalFeatures);
        }

        System.out.println("Total price of fifth, sixth and seventh vehicles is : " + price);

        for (Vehicle vh : v) {
            System.out.println(vh.toString());
        }
    }
}

Output

Total price of fifth, sixth and seventh vehicles is : 128900
Base price : 0, Additional Features : 0
Base price : 0, Additional Features : 0
Base price : 0, Additional Features : 0
Base price : 0, Additional Features : 0
Base price : 37000, Additional Features : 400
Base price : 24000, Additional Features : 5500
Base price : 53000, Additional Features : 9000

Process finished with exit code 0
Add a comment
Know the answer?
Add Answer to:
Part I: Create a program class named TestArrays This class should have a main() method that...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • create java application with the following specifications: -create an employee class with the following attibutes: name...

    create java application with the following specifications: -create an employee class with the following attibutes: name and salary -add constuctor with arguments to initialize the attributes to valid values -write corresponding get and set methods for the attributes -add a method in Employee called verify() that returns true/false and validates that the salary falls in range1,000.00-99,999.99 Add a test application class to allow the user to enter employee information and display output: -using input(either scanner or jOption), allow user to...

  • Equals and Copy method activity Implement a Dog class with the following features: + Attributes: ...

    Java Equals and Copy method activity Implement a Dog class with the following features: + Attributes: age, gender, weight + Method: constructor, copy constructor, equals (if same weight and gender), toString + A main method/driver that create an array of 5 Dog objects (obtain input from user), and then display the objects that are "equal" Equals and Copy method activity Implement a Dog class with the following features: + Attributes: age, gender, weight + Method: constructor, copy constructor, equals (if...

  • In C++ Write a program that contains a class called VideoGame. The class should contain the...

    In C++ Write a program that contains a class called VideoGame. The class should contain the member variables: Name price rating Specifications: Dynamically allocate all member variables. Write get/set methods for all member variables. Write a constructor that takes three parameters and initializes the member variables. Write a destructor. Add code to the destructor. In addition to any other code you may put in the destructor you should also add a cout statement that will print the message “Destructor Called”....

  • Create a java project and create Student class. This class should have the following attributes, name...

    Create a java project and create Student class. This class should have the following attributes, name : String age : int id : String gender : String gpa : double and toString() method that returns the Student's detail. Your project should contain a TestStudent class where you get the student's info from user , create student's objects and save them in an arralist. You should save at least 10 student objects in this arraylist using loop statement. Then iterate through...

  • Write a c# console program called “ArrayOfThings” where you create a class called Staff with private...

    Write a c# console program called “ArrayOfThings” where you create a class called Staff with private fields for StaffID and StaffName. Create Properties that can write data and retrieve data from these private fields. Create a Staff array that can hold up to ten Staff objects. Create ten objects using a for loop that automatically creates a new (empty) Staff object at each iteration of the loop and adds that object into the Staff array. Write data into the first...

  • Python programming I need help with the following: Create a class Vehicle. The class should have...

    Python programming I need help with the following: Create a class Vehicle. The class should have name, weight, fuel, number of wheels and move attributes/methods. Create child class that are semi, car, sail boat and bicycle. Give the class the ability to have a constructor that sets the name. Have the other attributes hard coded to suit the vehicle. Give each a way of speaking their attributes as well as the type of animal. For example “Hi I am Lightening...

  • Programming 5_1: Create a Java class named <YourName>5_1 In this class create a main method...

    Programming 5_1: Create a Java class named <YourName>5_1 In this class create a main method, but for now leave it empty. Then write a Java method getAverage which takes an array of integers as it’s argument. The method calculate the average of all the elements in the array, and returns that average. The method should work for an array of integers of any length greater than or equal to one. The method signature is shown below: public static double getAverage(...

  • public class EmployeeDriver public static void main(String[] args) 1/declare create an Employee object named joo. Une...

    public class EmployeeDriver public static void main(String[] args) 1/declare create an Employee object named joo. Une no-arg constructor 1/change the attributes for joe as the following: //name to Joe Cool, id to 1111111, hours to 20, pay rate to 15 //dealare & create another Employee oblegt named one using arg-constructor // The name of the new employees Jane Doeid is 2001122. She makes $10.50/hour // change the object Jane's hours to 40. 1/change the object jane's pay rate to $12.00....

  • Look at the Account class below and write a main method in a different class to briefly experiment with some instances of the Account class.

    Look at the Account class below and write a main method in a different class to briefly experiment with some instances of the Account class.Using the Accountclass as a base class, write two derived classes called SavingsAccountand CurrentAccount.ASavingsAccountobject, in addition to the attributes of an Account object, should have an interest variable and a method which adds interest to the account. ACurrentAccount object, in addition to the instance variables of an Account object, should have an overdraft limit variable. Ensure...

  • java: 1d arrays PLEASE NEED HELp THANK YOU!!! One dimensional (1D) array 1. Create an array...

    java: 1d arrays PLEASE NEED HELp THANK YOU!!! One dimensional (1D) array 1. Create an array of 1000 integers. Name the array: x 2. Assign 95 to the ninth element, 25 to the twentieth element of array x. 3. Assign the sum of the ninth and the twentieth element to the sixtieth element of array x. 4. Display the sixtieth element of the array. 5. Use the for statement to generate all indexes to read and display all elements in...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT