Question

In: Computer Science

(USING JAVA) Implement a class Moth that models a moth flying along a straight line. The...

(USING JAVA)

Implement a class Moth that models a moth flying along a straight line. The moth has a position which is the distance from a fixed origin. When the moth moves toward a point of light its new position is halfway between its old position and the position of the light source. Supply a constructor

public Moth(double initialPosition)

and methods

public void moveToLight(double lightPosition)

public double getPosition()

Solutions

Expert Solution

Explanation of the code is explained in the comments of he code itself.

Code--

import java.util.*;
class Moth
{
private double initialPosition;
//constructor
public Moth(double initialPosition)
{
this.initialPosition=initialPosition;
}
//required method
public void moveToLight(double lightPosition)
{
initialPosition=(initialPosition+lightPosition)/2;
}
//getter
public double getPosition()
{
return initialPosition;
}
}
public class Main
{
public static void main(String[] args)
{
//create an object of Moth
Moth myMoth=new Moth(66.6);
//display the current position
System.out.println("Initial position of moth = "+ myMoth.getPosition());
//call moveToLight
myMoth.moveToLight(22.6);
//Display the new pisition
System.out.println("New position of moth = "+ myMoth.getPosition());
  
}
}

Code Screenshot--

Output Screenshot--

Note--

Please upvote if you like the effort.


Related Solutions

0. Introduction. In this assignment you will implement a stack as a Java class, using a...
0. Introduction. In this assignment you will implement a stack as a Java class, using a linked list of nodes. Unlike the stack discussed in the lectures, however, your stack will be designed to efficiently handle repeated pushes of the same element. This shows that there are often many different ways to design the same data structure, and that a data structure should be designed for an anticipated pattern of use. 1. Theory. The most obvious way to represent a...
2. a. A particle moves in a straight line along with the ? − ???? its...
2. a. A particle moves in a straight line along with the ? − ???? its displacement is given by the equation ?(?) = 5? 3 − 8? 2 + 12? + 6,? ≥ 0, where ? is measured in seconds and s is measured in meters. Find: i. The velocity function of the particle at time ? (2marks) ii. The acceleration function of the particle at time t. (2marks) iii. The acceleration after 5 seconds (1marks) b. Find the...
Java Programming Part 1 (20%) Implement a class with a main method. Using an enhanced for...
Java Programming Part 1 (20%) Implement a class with a main method. Using an enhanced for loop, display each element of this array: String[] names = {"alice", "bob", "carla", "dennis", "earl", "felicia"}; Part 2 (30%) In a new class, implement two methods that will each calculate and return the average of an array of numeric values passed into it. Constraints: your two methods must have the same name one method should accept an array of ints; the other should accept...
You drop a package from a plane flying at constant speed in a straight line. Without...
You drop a package from a plane flying at constant speed in a straight line. Without air resistance, the package will. (Hint: think about the velocity along the x direction, think about what type of motion you have along x direction). A) quickly lag behind the plane while falling. B) remain vertically under the plane while falling. C) move ahead of the plane while falling. D) not fall at all.
Use Java programming to implement the following: Implement the following methods in the UnorderedList class for...
Use Java programming to implement the following: Implement the following methods in the UnorderedList class for managing a singly linked list that cannot contain duplicates. Default constructor Create an empty list i.e., head is null. boolean insert(int data) Insert the given data into the end of the list. If the insertion is successful, the function returns true; otherwise, returns false. boolean delete(int data) Delete the node that contains the given data from the list. If the deletion is successful, the...
using Java Implement a class called Binomial_Coefficient o Your class has 2 int attributes, namely K...
using Java Implement a class called Binomial_Coefficient o Your class has 2 int attributes, namely K and n o Create an overloaded constructor to initialize the variables into any positive integers such that n > k > 0o Create a method that calculates the value of binomial coefficient, C(n,k) , using the following rule: ▪ For an array of size (n+1) X (k+1) ▪ Array [n][k] = Array[n-1][ k-1]+ Array[n-1] [k]▪ Array[n][0]= Array[n][n] = 1 ▪ Hence, C(n,k) = array...
A molecule of DNA (deoxyribonucleic acid) lies along a straight line. It is 1.472
A molecule of DNA (deoxyribonucleic acid) lies along a straight line. It is 1.472
The position function of an object moving along a straight line is given by the function...
The position function of an object moving along a straight line is given by the function t3 - 15t2 -48t -10, where s is in metres and t is in seconds and 0≤15≤t . [7A] a) When is the velocity of the object greater than 21 m/s? b) When is the speed of the object less than 21 m/s? c) Illustrate the graphical representation for each of the above."
The displacement (in centimeters) of a particle moving back and forth along a straight line is...
The displacement (in centimeters) of a particle moving back and forth along a straight line is given by the equation of motion s = 3 sin(πt) + 4 cos(πt), where t is measured in seconds. (Round your answers to two decimal places.) (a) Find the average velocity during each time period. (i)    [1, 2] _______cm/s (ii)    [1, 1.1] _______ cm/s (iii)    [1, 1.01] ______ cm/s (iv)    [1, 1.001] _______cm/s B) Estimate the instantaneous velocity of the particle when t=1.
An object moves along a straight line. The position as a function of time is given...
An object moves along a straight line. The position as a function of time is given by the following 22 formula x(t)=5m+(2m/s)t–(0.6m/s)t^2. Please answer all of the parts, I will give a good rating**** :) How long after starting does it take the object to pass the origin? At what time is the object located 4 m from the origin? Sketch the following graphs: x(t), v(t), a(t). Find the object’s initial acceleration. At what time will the object come to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT