Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA Create three arrays of type double, of the same size. Initialize them in yo

ID: 3792010 • Letter: J

Question

JAVA

Create three arrays of type double, of the same size. Initialize them in your code. Write code that multiplies the values in the first two arrays and stores them in the third. Display the math and the answers.

Write a program that allows the user to enter a series of words into an array. Display the array in ascending alphabetic order.

Page 489 #3 - you will have to search the array to find the number entered.

Create a class called Planet. Store as fields the name of the planet, the gravity, and the length of the year. Create the appropriate constructors and accessors and toString. Build an array of this class. Create a program that displays the data on a planet based on user choice. For example, if the person selected Mars, then 3.71 m/s2 and 686 days.

Explanation / Answer


/**
* The java program ParallelArrays that initializes
* the array of type double of same size and finds
* the product of two areas and store in a new array
* of type double of same same size and print to console.
* */
//ParallelArrays.java
public class ParallelArrays {  
   public static void main(String[] args) {
      
       int size=10;
       //initialize the array of type double
       double[] length=new double[]{5.0,6.0,7.0,8.5,9.2,1.8,3.5,1.2,6.3,5.5};
       //initialize the array of type double
       double[] width=new double[]{5.0,6.0,7.0,8.5,9.2,1.8,3.5,1.2,6.3,5.5};
       //Declaration of array of type double
       double[] area=new double[size];
      
       //print length,widht and area to console
       for (int i = 0; i < size; i++)
       {
           area[i]=length[i]*width[i];
           System.out.printf("length %5.2f x width %5.2f= area %5.2f ",
                   length[i],width[i],area[i]);
       }
      
   }//end of main method

}//end of class ParallelArrays

sample output:

length 5.00 x width 5.00= area 25.00
length 6.00 x width 6.00= area 36.00
length 7.00 x width 7.00= area 49.00
length 8.50 x width 8.50= area 72.25
length 9.20 x width 9.20= area 84.64
length 1.80 x width 1.80= area 3.24
length 3.50 x width 3.50= area 12.25
length 1.20 x width 1.20= area 1.44
length 6.30 x width 6.30= area 39.69
length 5.50 x width 5.50= area 30.25

---------------------------------------------------------------------------------------------------------

/**
* The java program AscendingOrder that prompts user to
* enter five names and sorts the names in ascending order
* and prints the names to console
* */
///AscendingOrder.java
import java.util.Scanner;
public class AscendingOrder {
   public static void main(String[] args) {
      
       int size=5;
       String[] names=new String[size];
       Scanner scanner=new Scanner(System.in);
       for (int i = 0; i < names.length; i++) {
           System.out.printf("Enter name %d: ",(i+1));
           names[i]=scanner.nextLine();
       }
       //calling method sortascending
       sortascending(names);
      
       System.out.println("Ascending order sorted names");
       //print names to console
       for (int i = 0; i < names.length; i++) {
           System.out.println(names[i]);
       }
      
   }
  
   //Sorts the string array names in ascending order
   private static void sortascending(String[] names) {      
       for (int i = 0; i < names.length; i++) {
           for (int j = 0; j < names.length-1; j++) {
               if(names[j].compareTo(names[j+1])>0){
                   String temp=names[j];
                   names[j]=names[j+1];
                   names[j+1]=temp;
               }
           }
       }
      
   }

}

Sample output:
Enter name 1:
Sunday
Enter name 2:
Monday
Enter name 3:
Tuesday
Enter name 4:
Wednesday
Enter name 5:
Thursday
Ascending order sorted names
Monday
Sunday
Thursday
Tuesday
Wednesday


---------------------------------------------------------------------------------------------------------


//Planet.java
public class Planet {
  
   private String name;
   private double gravity;
   private int yearLength;
  
   //Consructor that takes name, gravity and yearLenght
   public Planet(String name,double gravity,int yearLength) {
       this.name=name;
       this.gravity=gravity;
       this.yearLength=yearLength;
   }
  
   //defautl constructor
   public Planet() {
      
   }

   public void setName(String name){
       this.name=name;
   }
   public void setGravity(double gravity){
       this.gravity=gravity;
   }
   public void setYear(int yearLength){
       this.yearLength=yearLength;
   }
  
  
   public String getName(){
       return name;
   }
   public double getGravity(){
       return gravity;
   }
   public int getYear(){
       return yearLength;
   }
  
   public boolean equals(String name) {      
       return this.name.equals(name);
   }
  
   //Returns the string representation of planet
   public String toString() {      
       return name+" gravity "+gravity+" m/s2 and "+yearLength+" days.";
   }

}


//PlanetTester.java
import java.util.Scanner;
public class PlanetTester {
   public static void main(String[] args) {


       //Create an array of Planet class
       Planet[] planets=new Planet[3];
       Planet searchPlanet=new Planet();


       //Set planet objects
       planets[0]=new Planet("Mars", 3.71, 686);
       planets[1]=new Planet("Moon", 0.0253, 27);
       planets[2]=new Planet("Earth", 9.8, 365);

       Scanner scanner=new Scanner(System.in);
       System.out.println("Enter name of planet :");
       //prompt for planet name
       String planetName=scanner.nextLine();
       boolean found=false;
       for (Planet planet : planets) {
           if(planet.getName().equals(planetName))
           {
               searchPlanet=planet;
               found=true;
           }
       }
       if(found)
           //print planet to console
           System.out.println(searchPlanet);
       else
           System.out.println("Planet not found");
   }
}//end of the class PlanetTester

Sample Output:

Enter name of planet :
Mars
Mars gravity 3.71 m/s2 and 686 days.