Question

Implement a class Moth that models a moth flying along a straight line. The moth has...

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.

public Moth(double initialPosition)
public void moveToLight(double lightPosition)
public double getPosition()

Homework Answers

Answer #1

All Class are Implemented in Java and executed

import java.io.*;
import java.util.*;
class Moth
{
private double position;
public Moth(double initialPostion)
{
position = initialPostion;
}
public void moveToLight(double lightPosition)
{
position = (position + lightPosition);
}

public double getPosition()
{
return position;
}
}
public class MothTester
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter Initial Position");
int n = sc.nextInt();
System.out.println("Enter New Position");
float b = sc.nextFloat();
Moth moth = new Moth(n);
System.out.println("Initial Position of the Moth =" +moth.getPosition());
moth.moveToLight(b);
System.out.println("New Position of Moth =" +moth.getPosition());
}
}
Know the answer?
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
Similar Questions
A car moves along a straight line with velocity, in feet/second, given by v(t)=35-5t for t≥0....
A car moves along a straight line with velocity, in feet/second, given by v(t)=35-5t for t≥0. (a) With forward being the direction of positive velocity, when is the car moving forward? Backward? Stopped? The car is moving forward for ≤t< . The car is moving backward for t> . The car is not moving at the instant t= . (b) Does the car ever return to its starting point? If so, when? If the car never returns to its starting...
Do the TODOs in SongFileAccessor.java. It inherits from FileAccessor class. TODO 1: Implement the processLine method....
Do the TODOs in SongFileAccessor.java. It inherits from FileAccessor class. TODO 1: Implement the processLine method. When the text file is processed, each line of text will be passed to processLine . Each line contains 4 fields: title, album, artist, and play time. The album field is optional. Each field is separated by a comma. TODO 2: Implement the songToCSVString method. This method takes a Song object as a parameter and returns a String which is the csv representation of...
Anna and Bonnie are located on a straight line in the following way: Anna is standing...
Anna and Bonnie are located on a straight line in the following way: Anna is standing at the origin of the x-axis and when Bonnie is located 20 m away in the positive x direction, running at the constant speed of 4.6m/s toward Anna, Anna begins running toward Bonnie with a constant acceleration of 1.8m/s2 . (a) Using the diagram below, provide all information given (i.e. Anna’s and Bonnie’s initial velocities and accelerations, marking any vectors in the diagram corresponding...
AP Computer Science A FRQ SHOW ALL YOUR WORK. REMEMBER THAT PROGRAM SEGMENTS ARE TO BE...
AP Computer Science A FRQ SHOW ALL YOUR WORK. REMEMBER THAT PROGRAM SEGMENTS ARE TO BE WRITTEN IN JAVA. Assume that the classes listed in the Java Quick Reference have been imported where appropriate. Unless otherwise noted in the question, assume that parameters in method calls are not null and that methods are called only when their preconditions are satisfied. In writing solutions for each question, you may use any of the accessible methods that are listed in classes defined...
Play musical chords (JAVA PROGRAMMING) Notes and frequencies Every musical note has a name and a...
Play musical chords (JAVA PROGRAMMING) Notes and frequencies Every musical note has a name and a frequency. You are given a file where each line contains a note name, a tab character, and a frequence, which is a floating point value. For example, here are the first few lines from the file notes_frequencies.txt: A0 27.5 A#0 29.1353 B0 30.8677 C1 32.7032 C#1 34.6479 D1 36.7081 D#1 38.8909 E1 41.2035 F1 43.6536 Playing chords Write a program named PlayChords that first...
Compile and execute the application. You will discover that is has a bug in it -...
Compile and execute the application. You will discover that is has a bug in it - the filled checkbox has no effect - filled shapes are not drawn. Your first task is to debug the starter application so that it correctly draws filled shapes. The bug can be corrected with three characters at one location in the code. Java 2D introduces many new capabilities for creating unique and impressive graphics. We’ll add a small subset of these features to the...
Item 1 In the case below, the original source material is given along with a sample...
Item 1 In the case below, the original source material is given along with a sample of student work. Determine the type of plagiarism by clicking the appropriate radio button. Original Source Material Student Version There is a design methodology called rapid prototyping, which has been used successfully in software engineering. Given similarities between software design and instructional design, we argue that rapid prototyping is a viable method for instructional design, especially for computer-based instruction. References: Tripp, S. D., &...
Item 1 In the case below, the original source material is given along with a sample...
Item 1 In the case below, the original source material is given along with a sample of student work. Determine the type of plagiarism by clicking the appropriate radio button. Original Source Material Student Version There is a desperate need for theorists and researchers to generate and refine a new breed of learning-focused instructional design theoriesthat help educators and trainers to meet those needs, (i.e., that focus on learning and that foster development of initiative, teamwork, thinking skills, and diversity)....
Item 1 In the case below, the original source material is given along with a sample...
Item 1 In the case below, the original source material is given along with a sample of student work. Determine the type of plagiarism by clicking the appropriate radio button. Original Source Material Student Version I accept the point that whenever learning occurs, some medium or mix of media must be present to deliver instruction. However, if learning occurs as a result of exposure to any media, the learning is caused by the instructional method embedded in the media presentation....
Item 1 In the case below, the original source material is given along with a sample...
Item 1 In the case below, the original source material is given along with a sample of student work. Determine the type of plagiarism by clicking the appropriate radio button. Original Source Material Student Version Major changes within organizations are usually initiated by those who are in power. Such decision-makers sponsor the change and then appoint someone else - perhaps the director of training - to be responsible for implementing and managing change. Whether the appointed change agent is in...