Exit Code 0 instead of printing? - java

I have the following java code written and when I run it it says: "Process finished with exit code 0" instead of printing totalNumCars(). Why is this, and how do I fix it?
public class ParkingGarage extends Module5{
public int numAutomobiles;
public int numLargeAutomobiles;
public static int numGarages;
public ParkingGarage(int automobiles, int largeAutomobiles){
numAutomobiles = automobiles;
numLargeAutomobiles = largeAutomobiles;
numGarages++;
}
public void addCars(int newAutomobiles, int newLargeAutomobiles){
numAutomobiles += newAutomobiles;
numLargeAutomobiles += newLargeAutomobiles;
}
public int totalNumCars(){
int totalCars = numLargeAutomobiles + numAutomobiles;
return totalCars;
}
public static void main(String[] args){
ParkingGarage garage1 = new ParkingGarage(5,5);
garage1.addCars(5, 5);
System.out.println("Number of Cars: " + garage1.totalNumCars());
}
}

Related

Auto increment for transaction number (Eclipse Java)

I want to get increasing value starting with 1 so that I get TK-1 all the way to TK-n.
What I've tried is:
public class Main {
addTicket();
public void addTicket() {
int a;
String ticket;
a = getA();
ticket = "TK-"+ a
System.out.println(ticket)
}
public int getA() {
int a, b;
a = 0;
b = a++;
return a;
public static void main(String[] args) {
new Main();
}
}
Sorry it's my first time learning to code, can someone explain to me why it isn't working and what should I do to make it work ?
Thanks in advance.
If you need an autoincrease ID, you need a static int:
public class Ticket {
static int ticketID = 1;
public void addTicket() {
int a;
String ticket;
a = getticketID();
ticket = "TK-" + a;
System.out.println(ticket);
}
public int getticketID () {
return ticketID++;
}
public static void main(String[] args) {
Ticket test = new Ticket();
test.addTicket();
test.addTicket();
test.addTicket();
}
}
output:
enter image description here

How can I Connect these two classes?

How can I connect these two classes with eath other, so "17-0.10" comes out at Class 1?
Class 1:
public class Main {
public static int String(int room){
}
public static void main(String[] args) {
Room office = new Room(17, 0, 10);
Room lecture = new Room(2, 0, 10);
Room lab = new Room(18, 1, 1);
System.out.println(office); // => "17-0.10"
System.out.println(lecture); // => " 2-0.10"
System.out.println(lab); // => "18-1.01"
}
}
Class 2:
public class Room {
public int gebaeude;
public int etage;
public int raumnummer;
public Room(int gb, int eg, int rn) {
this.gebaeude = gb;
this.etage = eg;
this.raumnummer = rn;
}
public String toString() {
return this.gebaeude + "-" + this.etage + "." + this.raumnummer;
}
}
If you asked, i wrote Class 2, and "public static int String(int room)", I am new in Java, the first thing that i tried was just System.out.println (Room);
In your code, just remove the String() method in Main class. Everything else is working fine.

Need some revision on this code.

Help, I need some revision on this code. How do I get the values of 65 and 106 without removing it from Exercise3 myExer3 = new Exercise3(65,106);
Base Class:
public class Exercise3 {
private int Voltage;
private int Resistance;
public void setVoltage(int temp){
if (Voltage == 65)
Voltage = temp;
}
public void setResistance(int temp){
if (Resistance == 106 )
Resistance =106;
}
public int getVoltage (){
return (Voltage);
}
public int getResistance(){
return(Resistance);
}
}
Test Class:
public class Test_Excercise3 {
public static void main(String []args){
Exercise3 myExer3 = new Exercise3(65,106);
System.out.println("Voltage: "+myExer3.getVoltage());
System.out.println("Resistance: "+myExer3.getResistance());
System.out.println("Current : "+ (myExer3.getVoltage()/myExer3.getResistance()));
}
}
So that I could get the result of 0.61 Ohm's or the Current.
Your class needs a constructor:
public class Exercise3 {
private int voltage;
private int resistance;
public Exercise3(int voltage, int resistance) {
this.voltage = voltage;
this.resistance = resistance;
}
...
}
For more information, consult the Java Tutorials on providing constructors for your classes.
Add a constructor to the Excercise3 and correct setters, also do conversion to (double) of the result.
public class JavaApplication27
{
public static class Exercise3
{
private int voltage;
private int resistance;
public void setVoltage(int v)
{
voltage = v;
}
public void setResistance(int res)
{
resistance = res;
}
public int getVoltage()
{
return voltage;
}
public int getResistance()
{
return resistance;
}
public Exercise3(int v, int res)
{
setVoltage(v);
setResistance(res);
}
public double getCurrent() //helper method :)
{
return (double) getVoltage() / getResistance();
}
}
public static void main(String[] args)
{
Exercise3 myExer3 = new Exercise3(65, 106);
System.out.println("Voltage : " + myExer3.getVoltage());
System.out.println("Resistance: " + myExer3.getResistance());
System.out.println("Current : " + ( (double) myExer3.getVoltage() / myExer3.getResistance())); // Since resistance and voltage are int's, the result of int/int division is int. To get a double) result use (double) :).
System.out.println("Current : " + myExer3.getCurrent()); //you may also use helper method to calculate current
System.out.format( "Current : %.2f", myExer3.getCurrent() ); // to get .61 must use formatter
}
}
Output:
Voltage : 65
Resistance: 106
Current : 0.6132075471698113
Current : 0.6132075471698113
Current : 0.61

Java Error - Missing method body or declare abstract

emphasized textI've been working on this problem for a while and managed to get rid of almost all the errors on this class. This error keeps saying I'm missing method body or declare abstract but I just don't see it. I've managed to complete another class almost similar to this but this one seems to be acting strangely. Can someone please help me out? Thank you if you do.
import java.util.Scanner;
public class HockeyPlayer extends StudentAthlete
{
Scanner keyboard = new Scanner(System.in);
public static void main (String [] args)
{
HockeyPlayer athlete1 = new HockeyPlayer("Dave", 111111, 15, 3.2, 2, 3);
athlete1.writeOutput();
}
private int assist = 0;
private int goal = 0;
public HockeyPlayer()
{
super();
goal = 0;
assist = 0;
}
public int getAssist()
{
return assist;
}
public void setAssist(int newAssist)
{
if (0 >= newAssist)
{
assist = newAssist;
}
else
{
System.out.println("Invalid Assists");
System.out.println("Please enter a valid Assists");
int tempAssist = keyboard.nextInt();
setAssist(tempAssist);
}
}
public int getGoal()
{
return goal;
}
public int setGoal(int newGoal)
{
if (0 >= newGoal)
{
goal = newGoal;
}
else
{
System.out.println("Invalid Goals");
System.out.println("Please enter a valid Goals");
int tempGoal = keyboard.nextInt();
setGoal(tempGoal);
}
}
public HockeyPlayer(String initialName, int initialStudentNumber, int initialJersey, double initialGpa, int initialGoal, int initialAssist)
{
super (initialName, initialStudentNumber,initialJersey, initialGpa);
setGoal(initialGoal);
setAssist(initialAssist);
}
public HockeyPlayer(String initialName, int initialStudentNumber, int initialJersey, double initialGpa)
{
super (initialName, initialStudentNumber, initialJersey, initialGpa);
goal = 0;
assist= 0;
}
public HockeyPlayer(String initialName, int initialStudentNumber)
{
super (initialName, initialStudentNumber);
goal = 0;
assist = 0;
}
public HockeyPlayer(String initialName)
{
super(initialName);
goal = 0;
assist = 0;
}
public void writeOutput(); // THE ERROR OCCURS HERE
{
super.writeOutput();
System.out.println("Goals: " + goal);
system.out.println("Assists: " + assist);
}
}
change
public int setGoal(int newGoal)
to
public void setGoal(int newGoal)
Setter methods usually don't have a return type (and based on the fact that you don't try to return anything, you probably didn't intend it to have an int return type).
Also change
public void writeOutput();
to
public void writeOutput()

Random generator does not work it always shows 0

import java.util.Random;
public class PouAbilites {
protected int Health;
public int getHealth(){
return this.Health;
}
public void setHealth(final int Health){
final Random random = new Random();
final int randomInt = random.nextInt(100)+1;
this.Health=randomInt;
}
PouAbilites(){
this.Eletero=getEletero();
}
public void onscreen(){
System.out.println("Health: "+ this.Health);
}
}
The main function is included in an other class:
package KotelezoProgram;
public class Main {
public static void main(final String[] args) {
final PouAbilites Pou = new PouAbilites();
Pou.onscreen();
}
}
call setHealth() in between
PouAbilites Pou = new PouAbilites();
Pou.onscreen();
calls
Calling onscreen() before setHealth() will return 0 because this.Health is just initialized in the Pou object to zero.

Categories