1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
deff fn [24]
4 years ago
15

Name the different types of constructors that a designer can provide in a class. Provide an example with a class showing many co

nstructors.
Computers and Technology
1 answer:
Marrrta [24]4 years ago
8 0
By default, if you do not implement a constructor, the compiler will use an empty constructor (no parameters and no code). The following code will create an instance of the MyObject class using the default constructor. The object will have the default vauesfor all the attributes since no parameters were given.

MyObject obj = new MyObject();

Another type of constructor is one with no parameters (no-arg constructor). It is similar to the default, except you actually create this constructor. The contents of the the constructor may include anything. To call a no-arg constructor, use the same line of code as above. The constructor can look like the one below:

public MyObject() {
System.out.println("This is a no-arg constructor");
}

Lastly there is the parameterized constructor. This type of constructor takes in parameters as inputs to assign to values in the newly created object. You call a parameterized constructor as follows:

MyObject obj = new MyObject("Bob", 20);

The constructor will look like this:

public MyObject(String name, int age) {
this.name = name;
this.age = age;
}

In the constructor, the keyword "this" refers to the object, so this.name is a private global variable that is being set equal to the inputted value for name, in this case "Bob".

Hope this helps!
You might be interested in
Select the correct line of missing code to create an output of "hello."
s2008m [1.1K]

Answer:

B

Explanation:

This feels like an error on the question issuer's part.

3 0
3 years ago
Your first job is to create a Java program that repeatedly asks the user whether they wish to calculate another square root. If
Zinaida [17]

Your first job is to create a Java program that repeatedly asks the user whether they wish to calculate another square root. If the response is "y", then the program should proceed; if it is anything else, then the program should quit. Whenever it proceeds, the program should prompt the user for a number (a positive double, and your program may simply assume the input is consistent with this requirement) and then report the square root of that number to within a relative error of no more than 0.01%. The computation must be done using Newton iteration.

The intuitive idea of Newton iteration for computing square roots is fairly straightforward. Suppose you have a guess r for x1/2 that is too large; the argument is similar if it is too small. If r is too large to be the square root of x, then x/r must be too small, so the average of r and x/r should be a better guess than either r or x/r. This suggests that if you repeatedly replace your current guess r by (r + x/r)/2, then your sequence of guesses should converge to x1/2. And indeed it can be proved that it does. A good initial guess for x1/2 is simply r = x. If you continue updatingr until |r2 – x |/x < ε2, then the relative error of the guess r will be less than ε.

After your initial program works, there are a number of other requirements to change it slightly, one step at a time, as explained below.

Open the src folder of this project and then open (default package). As a starting point you should useProgramWithIOAndStaticMethod.java. Rename it Newton1 and delete the other files from the project (if needed, see Creating a Program from a Skeleton (also Renaming a Java Program) for details).

Edit Newton1.java to satisfy the problem requirements stated above, including updating comments appropriately. Estimating the square root should be done in a static method declared as follows:

1

2

3

4

5

6

7

8

9

10

/**

* Computes estimate of square root of x to within relative error 0.01%.

*

* @param x

*            positive number to compute square root of

* @return estimate of square root

*/

private static double sqrt(double x) {

   ...

}

Copy Newton1.java to create Newton2.java. Change sqrt (including its Javadoc comments) so it also works when x = 0.

Copy Newton2.java to create Newton3.java. Change it so the main program prompts the user to input the value of ε (rather than assuming it is 0.0001), just once as the program begins, and so this value is also passed to sqrt.

Copy Newton3.java to create Newton4.java. Change it so the main program does not ask the user whether they wish to calculate another square root, but instead simply asks for a new value of x and interprets a negative value as an indication that it's time to quit.

Select your Eclipse project Newton (not just some of the files, but the whole project), create a zip archive of it, and submit the zip archive to the Carmen dropbox for this project, as described in Submitting a Project.

7 0
3 years ago
What is the difference between software and operating system software?
Basile [38]

Answer:

System software is the software that manages the resources and allows a user to interact with the system. On the other hand, an operating system collects programs that coordinate all activities among computer hardware devices.

4 0
3 years ago
In order to get a great sports photograph, you need to do what?
Alik [6]
The answer is Anticipate the action.  <span>In order to get a great sports photograph, you need to anticipate the action.  </span><span>A big part of </span>sports photography<span> is </span>anticipating action.  <span>The photographer will be able to anticipate the action so that the camera will be pointed in the right direction to capture the decisive moments on film</span>
8 0
4 years ago
Read 2 more answers
Complete the code to finish this program to analyze the inventory for a store that sells purses and backpacks.
suter [353]

Answer:

import <em>csv</em>

fileIn = open("data/bags.txt","r")

countPurse = 0

Explanation:

Edge 2021

4 0
3 years ago
Other questions:
  • Describe the benefits and drawback of using virtualisation on a single machine and also in a cluster environment
    10·1 answer
  • An ideal line length would include how many characters? A. 6570 B. 100 C. 100125 D. 4055
    8·1 answer
  • 4-One possible performance multiplication enhancement is to do a shift and add instead of an actual multiplication. Since 9 * 6,
    12·1 answer
  • What is the most useful advantage of using the Slide Sorter view to rearrange slides instead of using the slide thumbnails in No
    13·1 answer
  • An anchor tag can be coded using which two attributes? HELP ME PLEASE!!!!
    12·2 answers
  • This software application can be used to organize, analyze, and illustrate data?
    12·2 answers
  • What are the parts of word?
    7·1 answer
  • What are the components of a web page???
    9·2 answers
  • How do you believe cryptocurrency work shape the next 10 years
    6·1 answer
  • How do I type in bold on brainly?
    8·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!