Implement queues in java [duplicate] - java

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 7 years ago.
I have a homework about implementing queues in java. I have written a code, but there is an error and I don't know how to fix it. Can please anyone help me with this?
Here is my code:
public class Radha {
public int num;
public Radha pas;
public Radha(int num){
this.num = num;
}
public void shfaq(){
System.out.println(num);
}
public static void main (String [] args){
Radha x = new Radha(1);
Radhe1 r = x.new Radhe1();
r.enqueue(1);
r.shfaq();
}
class Radhe1{
public Radha koka;
public Radha bishti;
Radhe1(){
koka.pas = null;
bishti = koka;
}
public void shfaq(){
Radha theLink = koka;
while(theLink != bishti){
theLink.shfaq();
theLink = theLink.pas;
}
}
public boolean bosh(){
return(bishti == koka);
}
public int iPari (){
if (bosh())
System.out.println("radha eshte bosh");
return(koka.num);
}
public void dequeue(){
if (bosh()){
System.out.println("radha eshte bosh");
}
else{
koka = koka.pas;
}
}
public void enqueue(int a){
bishti = bishti.pas;
bishti.num = a;
bishti.pas = null;
}
}
}

When you write koka.pas = null, there is no koka whose pas you can set. You must initialize that somehow.

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

Exit Code 0 instead of printing?

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());
}
}

I'm trying to find the sum of the objects that are defined as numbers

I'm doing an exercise, in which I have to create the method add, however due to p and v being defined as objects, I'm having a hard time figuring out how I can define this method in the syntax I've been given in the exercise (I'm only allowed to change the methods).
I would like to add the two inputs 5 and 17 so that it returns 22. I've done a lot of research into other questions where I've seen them write it as Positiv(p + v) but this doesn't quite work.
public class Positiv {
public static void main(String[] args) {
try {
Positiv p = new Positiv(5);
Positiv v = new Positiv(17);
p.add(v);
System.out.println(p.get());
} catch (Exception e) {
e.printStackTrace();
}
}
private int n;
public Positiv(int n) {
if (n < 0) { throw new IllegalArgumentException("exception");
}
this.n = n;
}
public static Positiv add(Positiv v)
{
return new Positiv(n + v);
}
public int get() {
return n;
}
}
In your add method:
public static Positiv add(Positiv v)
{
return new Positiv(n + v);
}
You return a whole new Positiv object. However (correct me if I'm wrong) it looks as if you just want to add the two n fields. You can do this by adding this.get to v.get:
public void add(Positiv v)
{
this.n += v.get();
}
Which will return 22
Tutorial for this
public class HelloWorld{
public static void main(String []args){
Numbers a = new Numbers(5);
Numbers b = new Numbers(10);
int num1 = a.getN();
int num2 = b.getN();
System.out.println(addTwoNumbers(num1, num2));
}
public static int addTwoNumbers(int a, int b) {
return a + b;
}
}
class Numbers {
private int n;
public Numbers(int n) {
this.n = n;
}
public int getN() {
return n;
}
}

Calling a method from main method [duplicate]

This question already has answers here:
cannot make a static reference to the non-static field
(7 answers)
Closed 7 years ago.
When I am calling a method directly from main method, it is not allowed. However, when I am calling the same method from the constructor of a class, it is allowed.
The allowed version;
public class App {
Integer firstVariable;
Integer secondVariable;
public static void main(String[] args) {
App obj = new App(3, 2);
}
public App(Integer firstVariable, Integer secondVariable) {
this.firstVariable = firstVariable;
this.secondVariable = secondVariable;
this.calculate(firstVariable, secondVariable);
}
public int calculate(int a, int b) {
int result = a + b;
return result;
}
}
The disallowed version;
public class App {
Integer firstVariable;
Integer secondVariable;
public static void main(String[] args) {
App obj = new App(3, 2);
obj.calculate(firstVariable, secondVariable);
}
public App(Integer firstVariable, Integer secondVariable) {
this.firstVariable = firstVariable;
this.secondVariable = secondVariable;
}
public int calculate(int a, int b) {
int result = a + b;
return result;
}
}
I know it is "Cannot make a static reference to the non-static field firstVariable" error. My question is; In both code blocks, the same thing is done but what is the difference between them?
The issue isn't your method. The issue is that your variables (the arguments that you're trying to pass) are being referenced from a static context.

Code gives NullPointerException [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 7 years ago.
I have made five classes (as shown below). When I run my code it gives me "NullPointerException"
First class
public class PasPosition {
boolean lessThanThirty;
boolean ThirtyToFifty;
boolean FiftyToHundred;
public PasPosition(boolean value1, boolean value2, boolean value3)
{
lessThanThirty = value1;
ThirtyToFifty = value2;
FiftyToHundred = value3;
}
}
Second Class
public class CssPosition {
boolean lessThanThirty;
boolean ThirtyToFifty;
boolean FiftyToHundred;
public CssPosition(boolean value1,boolean value2, boolean value3)
{
lessThanThirty = value1;
ThirtyToFifty = value2;
FiftyToHundred = value3;
}
}
Third class
public class SiteData {
PasPosition pas;
CssPosition css;
}
last class
public class Test {
SiteData[] sitedata = new SiteData[2];
public void test()
{
for(int i=0;i<sitedata.length;i++)
{
System.out.println(sitedata[i].css.FiftyToHundred);
System.out.println(sitedata[i].css.ThirtyToFifty);
System.out.println(sitedata[i].css.lessThanThirty);
System.out.println();
}
}
}
It seems that you didn't init the objects.
You must know that in java, an object in a class will not be automatically inited. You must use new operator.
public class SiteData {
PasPosition pas=new PasPosition(false,false,false);
CssPosition css=new CssPosition(false,false,false);//use your own initial value
}

Categories