I need to know why this app's output is : 1243 [closed] - java

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
class A{
public A(int i) {
System.out.println(1);
}
public A() {
this(10);
System.out.println(2);
}
void A() {
A(10);
System.out.println(3);
}
void A(int i) {
System.out.println(4);
}
}
public class MainClass{
public static void main(String[] args) {
new A().A();
}
}
I don't understand the output of this code. I am a beginner and I really want to know why is this happening, the execution track or what happens when this code runs?

You create new instance of A class with empty constructor. new A()
In empty constructor, first call is the constructor with the value this(10).
Int the constructor with int value you display "1". System.out.println(1)
Then it returns back to empty constructor and call "2". System.out.println(2)
Next, you call the method with no parameters in it. .A()
And it call the the method with parameters. A(10)
Then it prints "4". System.out.println(4)
Also returns back to previous method and print "3" as last operation. System.out.println(3)
https://i.imgur.com/i5l8kTA.png
Sorry for my English.

Related

How to swap two elements (generics) [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 days ago.
Improve this question
[![enter image description here][1]][1]
Hello, everyone. I am a little bit confused in swapping. Plaese correct my swap method. I have tried to solve it in my ownself
I tried to not use temp
Swap<Integer, String> integerStringSwap = new Swap<>();
----------------------------------------------------
public class Swap<A,B> {
private A first;
private B second;
public Swap(A first, B second) {
this.second=second;
this.first=first;
}
void setFirst(A first){
this.first=first;
}
void setSecond(B second){
this.second=second;
}
A getFirst(){
return first;
}
B getSecond(){
return second;
}
void swap(){
}
}````
[1]: https://i.stack.imgur.com/yN3tu.png

Why is the integer 0 when I create an object and get that integer value using getters and setters? [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 2 years ago.
Improve this question
public class Test {
public static void main(String[] args) {
Test2 t2 = new Test2();
t2.test();
System.out.println(t2.getNum());
}
}
public class Test2 {
private int num;
public void test() {
Test2 t = new Test2();
t.setNum(3);
System.out.println(t.getNum());
}
public int getNum() {
return num;
}
public void setNum(int bruh) {
this.num = bruh;
}
}
Whenever I create an object of Test2 and set the value of num using t2.test(), It prints out on the console as 0 when using t2.getNum(). Why does it return a value of 0? And how do I fix it so it actually gets the value I want? Class Test and Test2 are separate files as a side note.
public void test() {
Test2 t = new Test2();
t.setNum(3);
System.out.println(t.getNum());
}
You create a new instance of Test2 locally in the method, which has no impact on this instance.
public void test() {
setNum(3);
}
would be enough.
t2 and t or 2 different objects.
When you call t2.test(), you create a new object t and you set the attribute num of the t object, not t2. So when you call t2.getNum(), the num attribute of t2 keep is default value which is zero for integer in Java.

Conditionally Throw exception [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
How do I make sure that some how it doesn't throw exception at first time while checking condition with 1 and allow to check all the condition and then throws the exception related to first Condition that "Number is already added".
class Hello
{
List<Integer> newExample=new Arraylist<Integer>();
List<Integer> example=new Arraylist<Integer>();
example.add(1);
example.add(2);
example.add(3);
public void addExample()
{
for(Integer ex: example)
{
pl.adding(ex);
}
}
public void adding(Integer example)
{
if(example==1)//some work around so that rest of list of example (2,3) get chance to execute.
{
throw new NumberAlreadyAddedException("Number is already added")
}
newExample.add(example);
}
}
I believe in your case you should be checking for duplicate input at the point in time where you are receiving/adding the values to the list. I have slightly modified your code to do the check for a duplicate at the point where you are about to add the value to the List and if the value is found to be a duplicate, thrown an error immediately:
import java.util.*;
class Hello{
List<Example> exam = new ArrayList<Example>();
public void execute() throws Exception{
add(1);
add(2);
add(3);
add(1);
for(Example ex:exam){
test(ex)
}
}
public void add(Integer value) throws IllegalArgumentException{
if(exam.contains(value)){
throw new IllegalArgumentException("Number is already added.");
}
exam.add(value);
}
public void test(Example example){
System.out.println("Running test on Example: " + example);//pl.test(ex);
}
public static void main(String[] args){
Hello hello = new Hello();
hello.add(1);
hello.add(2);
hello.add(3);
hello.add(1);
}
}
class Example{}

is there any way to call a variable from a specific if statement? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
ok so I need to make this program that involves the gravity constant. but i let the user decide that
double g;
String unit;
if (n == JOptionPane.YES_OPTION) {
g = 9.8;
System.out.print(g);
unit = "meters/s";
}
else {
g = 32;
System.out.print(g);
unit = "feet/s";
}
and then i put it into this formula OUTSIDE of the if statement
double ycoord = (velo0*sinF*time)-((g)((time*time)))/2;
i know that the scope of the if statement ends after the last curly brace but i'm wondering if there is any way to call for one of the values of g
thanks in advance!
If you have the above bit of code inside a method, then it's scope is restricted to that method. However you can create a class variable g and set it within your method.
Public Class Test {
//g can only be accessed within this class
//however you can access g with the following getter method
private double g;
public static void setG() {
this.g = 9.5;
}
public static void setGWithInput(Double input) {
this.g = input;
}
public static void printG() {
//you can access the value of g anywhere from your class
System.out.println("Value of g is" + this.g);
}
//create a public getter to access the value of g form outside the class
public double getG() {
return this.g;
}
}
As long as your statement containing your "formula" is within the same function/code block as the declaration of 'g', you can reference g as part of that statement.
You should really provide more details and describe your problem more explicitly.

Java arrays effective use/alternative [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
This is simple code which should demonstrate point of my question.I don't know if there are some more effective implementations of arrays than this one.What makes them so uncomfortable to me is that we have to write condition into if() block for each element and that makes code much longer.Is there any way how to include all elements of certain array in one line? Thank you!
public class test{
static String[] text;
public static void main(String args[]){
text=new String[5];
text[0]="hello";
...
text[4]="bye";
if(text[0].contains("something")==true||...text[4].contains("something")==true)
//do something
}
}
Either use a loop:
for (String item: text) {
if (item.contains("something")) {
// TODO condition
}
}
... or use an ugly workaround, such as:
if (Arrays.toString(text).contains("something")) {
// TODO condition
}
You can write your own method to do that:
public static anyContain(String[] array, CharSequence sub) {
for (String s : array)
if (s.contains(sub))
return true;
return false;
}
Then the if-statement will be
if (anyContain(text, "something")) {
...
}
I think this is the best way (only for full equals of elements):
text=new String[5];
...
if (Arrays.asList(text).contains("something")) {
// you code here
}
If you need test for contains you can also do:
String[] test = new String[] {"one", "two", "something", "three"};
Arrays.sort(test);
int index = Arrays.binarySearch(test, "something", new Comparator<String>() {
#Override
public int compare(String o1, String o2) {
return o1.contains(o2) ? 0 : -1;
}
});
As result you also get an index of element in array. Or negative value if not found.

Categories

Resources