Writing code in javadoc comment [duplicate] - java

This question already has answers here:
Multiple line code example in Javadoc comment
(18 answers)
Closed 9 years ago.
Is it possible to write a short code inside a java doc comment?
I don't mean a simple line of code, I mean a short method like:
public static void main() {
doSomething();
}

You can put your code in Multiple line comments like this.
/*
* public static void main() {
* doSomething();
* }
/
If you want to be in Java documentation comment,then it would be with two * after slash
/**
* public static void main() {
* doSomething();
* }
/

Related

How to capture the error message in private function [duplicate]

This question already has answers here:
JUnit test for System.out.println()
(14 answers)
Closed 5 months ago.
I have a function compare which called private function compareRuleRanks, and println the error or correct message when condition met, how can I capture the message when I do unit test? I try to use AssertionError but didn't work, how can I do that?
AssertionError(dataCompare.compare("Rule1", "Rule2"));
public void compare(){
compareRuleRanks(rule1, rule2);
}
private void compareRuleRanks(rule1, rule2) {
if(rule1.rank != rule2.rank) {
println("The ranks are not in order");
}
println("rules are same");
}
When unit-testing compareRulesRanks, you can definitely call the function but you would have to measure whether the intended reaction was triggered.
That means you would have to check whether println() got called as expected.
If you cannot do that you probably have to improve your code for testability.

method clear in class Screen cannot be applied to given types error [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 11 months ago.
Improve this question
I keep getting the error message
method clear in class Screen cannot be applied to given types;
required: boolean
found: no arguments
reason: actual and formal argument list differ in length
For clear(),
I'm trying to do an exercise from a workbook that wants me to use the following headers and fill out the methods and constructor. The actual function of clear() doesn't matter, just that I use the headers and call the clear method if xRes x yRes > 2000000)
The question,
Exercise 3.55 Given the following class (only shown in fragments here),
public class Screen
{
public Screen(int xRes, int yRes)
{ ...
}
public int numberOfPixels()
{ ...
}
public void clear(boolean invert)
{ ...
}
}
write some lines of Java code that create a Screen object. Then call its clear
method if (and only if) its number of pixels is greater than two million. (Don’t
worry about things being logical here; the goal is only to write something that is
syntactically correct—i.e., that would compile if we typed it in.)
My code,
/**
* Write a description of class Screen here.
*
* #author (your name)
* #version (a version number or a date)
*/
public class Screen
{
// instance variables - replace the example below with your own
private int xRes;
private int yRes;
private boolean invert;
/**
* Constructor for objects of class Screen
*/
public Screen(int xRes, int yRes, boolean invert)
{
// initialise instance variables
this.xRes = xRes;
this.yRes = yRes;
if((xRes * yRes) > 2000000)
{
clear(); // this is where i get my error
}
}
/**
* An example of a method - replace this comment with your own
*
* #param y a sample parameter for a method
* #return the sum of x and y
*/
public int numberOfPixels()
{
// put your code here
return xRes * yRes;
}
public void clear(boolean invert)
{
this.invert = invert;
}
}
The clear metod has a strange implementation right now as it doesn't clear anything but maybe you will get to fixing that later.
To call the method in it's current form you need to provide an argument of true or false.
Like this:
clear(true);
You need to pass a boolean value while invoking the method. It depends on you want do you want to pass. Call your method like this:
boolean a = true; // False can also be given depending on need
if((xRes * yRes) > 2000000)
{
clear(a);
}
You can pass either true or false by changing the value of a.

Java - metode inside a metode syntax issues [duplicate]

This question already has answers here:
Does Java support inner / local / sub methods?
(5 answers)
Closed 5 years ago.
Really don't know what it's called so I'm having a hard time searching for the answer.
Anyhow, I want to make a metode with metode inside (if that's even possible?).
public void log() {
public makeLogElement() {
//making a logelement to write inn
}
public write(String text) {
logelement.setText(logelement.getText() + text);
}
}
log myLog = new log();
myLog.makeLogElement();
myLog.write("This'll be written in the log");
What is the right syntax for making something like this?
It's not possible. But you can create a class inside a method.

Java replace line of output to standard out [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Write to same location in a console window with java
I was wondering if theres a way, in java, to replace a line of output you outputted to the terminal, such that you could do like a progress bar/counter type thing.
I'd like to do something akin to printing out "Records inserted 1/1000" and then "Records inserted 2/1000" over the top, replacing it so that only the most recent one shows.
Print the \r character, which places the cursor at the beginning of the line. And then write the new line.
public static void main(String[] args) throws InterruptedException {
System.out.print("test");
Thread.sleep(3000);
System.out.print('\r');
System.out.print("lulz");
}
Just rewire the System.out pipe to go through a filter of your own. e.g. System.setOut(new MyStream(System.out));
https://docs.oracle.com/javase/8/docs/api/java/lang/System.html#setOut-java.io.PrintStream-
You then need to implement MyStream:
public class MyStream extends PrintStream {
private PrintStream standardOut;
public MyStream(PrintStream standardOut) {
this.standardOut = standardOut;
}
... Then here override the appropriate methods (e.g. `println()`, etc...) to correct the output and send it to `standardOut`.
}

/* (non-javadoc) meaning [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Does “/* (non-javadoc)” have a well-understood meaning?
What does the following statements mean?
/* (non-Javadoc)
*
* Standard class loader method to load a class and resolve it.
*
* #see java.lang.ClassLoader#loadClass(java.lang.String)
*/
#SuppressWarnings("unchecked")
Javadoc looks for comments that start with /**.
By tradition, method comments that are not intended to be part of the java docs start with "/* (non-Javadoc)" (at least when your dev environment is Eclipse).
As an aside, avoid using multi-line comments inside methods. For example, avoid this:
public void iterateEdges()
{
int i = 0;
/*
* Repeat once for every side of the polygon.
*/
while (i < 4)
{
}
}
The following is preferred:
public void iterateEdges()
{
int i = 0;
// Repeat once for every side of the polygon.
while (i < 4)
{
++i;
}
}
The reason is that you open the possibility to comment out the entire method:
/*
public void iterateEdges()
{
int i = 0;
// Repeat once for every side of the polygon.
while (i < 4)
{
++i;
}
}
*/
public void iterateEdges()
{
// For each square edge.
for (int index = 0; index < 4; ++index)
{
}
}
Now you can still see the old method's behaviour while implementing the new method. This is also useful when debugging (to simplify the code).
I have seen this message generated by Eclipse when the programmer asks Eclipse to add a Javadoc comment to some code in a location where [EDIT: Eclipse thinks] the Javadoc tool will not actually use it.
A common example is the implementation of a method in an interface implemented by the class (which in Java 6 needs the #Override annotation). Javadoc will use the javadoc placed on the method in the INTERFACE, not the one provided in the implementation.
The rest of the comment was most likely written by a person that did not know this.
/*
* This is the typical structure of a multi-line Java comment.
*/
/**
* This is the typical structure of a multi-line JavaDoc comment.
* Note how this one starts with /**
*/
It's just a normal comment. The note means, if you create a manual, base of javadoc, this text won't be added.

Categories