Java Queries Here..

prateek007391

In the zone
Please check these codes :

public class Card {

private String face;
private String suit;

public Card(String cardFace, String cardSuit){

cardFace = face;
cardSuit = suit;

}

public String toString(){

return face + " of " + suit;
}
}

Code:
import java.util.Random;

public class DeckOfCards {

	private Card deck[];
	private int currentCard;
	private final int NUMBER_OF_CARDS = 52;
	private Random randomNumber;
	
	public DeckOfCards(){
		
		String faces[] = {"ACE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE", "TEN", "JACK", "QUEEN", "KING"};
		String suits[] = {"HEARTS", "DIMOND", "SPADES", "CLUBS"};
		
		deck = new Card[NUMBER_OF_CARDS];
		
		currentCard = 0;
		randomNumber = new Random();
		
		for(int count = 0; count<deck.length; count++){
			
			deck[ count ] = new Card(faces[ count % 13 ], suits[ count / 13 ]);
		}
		
	}
	
	public void shuffle(){
		
		currentCard = 0;
		
		for(int first = 0; first<deck.length; first++){
			
			int second = randomNumber.nextInt(NUMBER_OF_CARDS);
			
			Card temp = deck[first];
			deck[first] = deck[second];
			deck[second] = temp;
			
		}
	}
	public Card dealCard(){
		
		if(currentCard < deck.length )
			return deck[ currentCard++ ];
		else
			return null;
		}
}

Code:
public class DeckOfCardTest {

	
	public static void main(String[] args) {
		
		DeckOfCards myDeckOfCards = new DeckOfCards();
		myDeckOfCards.shuffle();
		
		
		for(int i = 0; i<13; i++ ){
			
			System.out.printf("%-20s%-20s%-20s%-20s\n", myDeckOfCards.dealCard(), myDeckOfCards.dealCard(),myDeckOfCards.dealCard(),myDeckOfCards.dealCard());
		}
	}

}

The Output must be Different cards but instead I get null of null 53 times.

Please help me, let me know whats wrong.

Sorry, got things Working.

Such a small Mistake and everything messes up.
 

braindead

Bankaiiiii
I just started using netbeans and dont know much about java either.
For the following code it shows "package com.amazonaws does not exist ...."
import com.amazonaws.AmazonClientException;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.auth.PropertiesCredentials;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
......
i think i have to add libraries or something like that. I have already downloaded aws-java-sdk-1.3.8. how do i add now? help !
 

masterkd

Padawan
Download proper .jar file(sorry, don't know which one you need here)..right click on your project..go to properties->java build path->add external jar->select the .jar file->click open and you're done!!
 

gopi_vbboy

Cyborg Agent
What is static branch in a class mean? Like below-



Code:
public class BarcodeEAN128 extends Barcode {

      //some variables decalration

       
         static {              
        final Map m = new HashMap();
         //some processing
           }

}
 

masterkd

Padawan
@braindead,
sorry..previous one i posted for eclipse
for netbeans, project properties->libreries->add jar

What is static branch in a class mean? Like below-



Code:
public class BarcodeEAN128 extends Barcode {

      //some variables decalration

       
         static {              
        final Map m = new HashMap();
         //some processing
           }

}

this is static block..codes inside this block will get executed first when the class is loaded!!
 

braindead

Bankaiiiii
^^ tried that , doesnt seem to work.

Now im desperate. Could someone please help me from the start.
Have a bunch of java programs already written in n++.
How do i import all these as a single project to either netbeans or eclipse or any other IDE?
And also add .jar file after that?
 

utkarsh73

Journeyman
I just had an overview of Java and I wanted to learn it thoroughly. So I started with Thinking in Java-3rd Edition HTML version but it is only for upto JDK 1.4. Will it pose any problems if I learn with this book or is it recommended to learn from a book updated with latest JDK??
 

vickybat

I am the night...I am...
Guys having trouble providing keyboard input as arguments to a function especially when passing more than 1 argument. Written a java code i procedural style ( without any use of objects) to show if a triangle can be formed when we provide three integer inputs.

The code works flawlessly when manually providing arguments to the method isTriangle.

But when using providing inputs through keyboard , it throws error. Providing the code below:


Code:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class triangleFormation {
public static boolean isTriangle (int a, int b, int c){
	if (c>a+b || a>b+c || b>a+c ){
		return false;
	} else{
		return true;
	}
}

public static void main (String [] args)throws IOException {
	
   BufferedReader bufferedreader = new BufferedReader(new InputStreamReader(System.in));
	String s = bufferedreader.readLine();
	String r = bufferedreader.readLine();
	String t = bufferedreader.readLine();
	
    int m = Integer.parseInt(s);
    int n = Integer.parseInt(r);
    int o = Integer.parseInt(t);
	
	
	
	System.out.println("the triangle is " + isTriangle (m,n,o));
}
}

I don't know if the process of providing keyboard input is correct or not. Only thing i found that its a hassle i java or i don't know some basic concepts. Providing inputs was so easy in c++.

Please throw some light into it guys. :)
 
Last edited:
Top Bottom