Can somebody write me some pseudo-code for a 2D array Java Nim Project?

February 15th, 2011 at 08:56am Under Forum

Can somebody write me some pseudo-code for a 2D array Java Nim Project?

“Project: Create the true game of Nim using a 2D array. The array should be 3×7. Initialize the appropriate cells to house stones and let users play the actual game. Here are the rules: On a players turn, they choose a row and can take as a many stones as they want, as long as the stones are adjacent(I cannot take the first and last stones), they cannot take multiple stones from different rows, and the last person to take a stone is the loser.” Can somebody give me steps in pseudocode or maybe even some “excerpts” from a program they have? Thanks.

By Barcode Scanner 2 comments

Urgent! I need help on a Java project for school! Can you fix this code?

February 15th, 2011 at 08:20am Under Forum

Urgent! I need help on a Java project for school! Can you fix this code?

I am working on a project for my AP Java (Computer Science A) class and I need your help! I am trying to post data to a html form from a particular website:

http://www.upcdatabase.com/itemform.asp

The Java code I wrote kind of sort of works, but returns the website source code that doesn’t contain the results of the query!

If I manually type in the following UPC code to the form: 075992584425

It will return: Description: Madonna – Like A Prayer
Size/Weight: CD … and a bunch of other corresponding data.

The Java Class code that I wrote for this:

// BEGIN CODE HERE ——————————————————————————–

import java.net.*;
import java.io.*;

public class ReadWriteURL {

public void PostToForm() {

try
{
//Create Post String
String data = URLEncoder.encode(“075992584425″, “UTF-8″);

System.out.println(“data= ” + data);
System.out.println();

// Send Data To Page
URL url = new URL(“http://www.upcdatabase.com/itemform.asp?”);

URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new

OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.flush();

// Get The Response
BufferedReader rd = new BufferedReader(new

InputStreamReader(conn.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
System.out.println(line);
//you Can Break The String Down Here
}
}

catch (Exception e) {
System.out.println(” There was an error”);
}
}

}

// END CODE ———————————————————————————————-

Can you quickly fix this code so that it retrieves the correct data, for the search value? (Same data like entering by hand)

I cannot figure out from the source code of the HTML page what to send or how to find out what the exact URL is to send it to.

By Barcode Scanner 1 comment

Sorting a 2d array in Java?

February 15th, 2011 at 08:20am Under Forum

Sorting a 2d array in Java?

I have to sort a 2d array:

int row=4;
int col=6;
int A[][]=new int [row][col];

With this info, I have to copy the components into a single array:
int B[][]=new int[24];
and sort it from highest to lowest, then copy it back into the 2d array.

so far, i have:import java.util.*;
import java.io.*;
import javax.swing.JOptionPane;

public class extraCredit{
public static void main (String [] args){
Scanner input= new Scanner(System.in);
int row=4;
int col=6;
int A[][]=new int [row][col];
int B[][]=new int[24];
int temp=0;
String out=”";

for(int i=0; i for(int j=0; j A[i][j]= input.nextInt();
}
}
//print original array
for(int i=0; i for(int j=0; j out+=”"+A[i][j];
}
out+= “\n”;
}
System.out.print(out);

for(int k=0; k for(int i=0;i for(int j=i; j if(A[i][c] A[i][c]=B[k]
}

I’m not sure how to finish it..

** I need to do this only using for loops and if statements .. without using the “.length” coding.

By Barcode Scanner 1 comment

how to write code of finding max. value in 2D Array in JAVA?

February 15th, 2011 at 08:05am Under Forum

how to write code of finding max. value in 2D Array in JAVA?

in java

for example,
int[][] array={{8,4,5,1}, {2,3,4,5},{10,9,4,3}};

how to write the code for finding max. or min value in this 2d array?
and has to show in this value is in which row and column.

By Barcode Scanner 1 comment

help with java language?! useing Scanner + 2D array?

November 30th, 2010 at 08:00pm Under Forum

help with java language?! useing Scanner + 2D array?

Suppose that temperature measurements were made on 7 days of 2009 in each of 5 cities.
write a program that will read the city name followed by the temperature measurements of that city.
the city name and measurements should be stored into two arrays.
the program should find out the average temperature for each city.
Also you should find the average temperature of each day.

By Barcode Scanner 1 comment

What's wrong with this java code?

May 4th, 2009 at 01:51pm Under Forum

What’s wrong with this java code?
//It won’t compile. Can anyone offer a fix, clean this up a bit?

import java.util.Scanner;

public class Calculator {

public static void main (String args[]) //throws Exception
{
Scanner keyboard = new Scanner(System.in);

System.out.println(“Enter the first number: “);
System.out.println(“the symbol, “);
System.out.println(“And the second number.”);
double num1 = keyboard.nextDouble();
String symbol = keyboard.nextLine();
double num2 = keyboard.nextDouble();
double answer;

if(symbol == “plus”)
{
answer = add(num1, num2);
}

else if(symbol == “minus”)
{
answer = subtract(num1, num2);
}

else if(symbol == “multiply”)
{
answer = multiply(num1, num2);
}

else if(symbol == “divide”)
{
answer = divide(num1, num2);
}

System.out.println(num1);
System.out.println(symbol);
System.out.println(num2);
System.out.println(” equals ” + answer);

}

public static double add(double num1, num2)
{
return num1+num2;
}

public static double subtract(double num1, num2)
{
return num1-num2;
}

public static double multiply(double num1, num2)
{
return num1*num2;
}

public static double divide(double num1, num2)
{
return num1/num2;
}

}

Best answer:

You have several problems:
Your program is relying on user input to continue, and this is always tricky.
This block here is a problem:
System.out.println(“Enter the first number: “);
System.out.println(“the symbol, “);
System.out.println(“And the second number.”);
double num1 = keyboard.nextDouble();
String symbol = keyboard.nextLine();
double num2 = keyboard.nextDouble();
double answer;
What happens if the user doesn’t press enter after the symbol?
Now, granted that won’t prevent the program from compiling, but it is a bug.
What kind of exception does the compiler throw?

By Barcode Scanner 1 comment

Can someone double check this java coding? I'm getting an error?

March 31st, 2009 at 07:48am Under Forum

Can someone double check this java coding? I’m getting an error?
i get the error: cannot find symbol – class Thermometer

import java.util.*;

public class TestProgThermometerTester
{
static Scanner console = new Scanner(System.in);

public static void main(String[] args)
{
Thermometer myThermometer = new Thermometer(35);
Thermometer yourThermometer =
new Thermometer();

int temperature;

System.out.println(“Line 10: myThermometer: ”
+ myThermometer);

System.out.println(“Line 11: yourThermometer: ”
+ yourThermometer);

yourThermometer.setCurrentTemperature(34);

System.out.println(“Line 13: After setting ”
+ “yourThermometer: ”
+ yourThermometer);

System.out.print(“Line 14: Enter the ”
+ “current temperature: “);

temperature = console.nextInt();
System.out.println();

myThermometer.setCurrentTemperature(temperature);

System.out.println(“Line 18: myThermometer: ”
+ myThermometer );

}//end main
}
If someone could tell me how to fix it too I would appreciate it. I’m taking a java class and pretty new to this.

Best answer:

you haven’t created a class named “Thermometer”, You have only created a main class called “TestProgThermometerTester”.

By Barcode Scanner Add comment

Java programming- Help needed!?

March 25th, 2009 at 05:11pm Under Forum

Java programming- Help needed!?
At present, I am currently unable to compile the program which I have been working on, as I keep getting the error of: “can not find symbol-variable i”.

Here is my code:

import java.util.Scanner;

class jets24
{
public static void main (String[]args)
{
Scanner input= new Scanner (System.in);

//variables//

int TotalLuxurySeats = 4 ;
int TotalExecutiveSeats = 6 ;
int Option = 0 ;
int LuxurySeatsSold=0;
int ExectutiveSeatsSold=0;
String[] CustomerName= new String [10];

// seat objectives within the array//

for(int i=0; i<10; i++)
{
if (i<4)
CustomerName[i]=new String ("Luxury");
else
CustomerName[i]=new String ("Exectutive");
}
while(Option!=4 || LuxurySeatsSold>0 || ExectutiveSeatsSold>0)
{
System.out.println(” Hello and welcome to CCNJet! “);

System.out.println(” “);

System.out.println(” Please select an option from the following menu “);
System.out.println(” “);
System.out.println(” 1. Book a luxury seat “);
System.out.println(” 2. Book an exectutive seat “);
System.out.println(” 3. Display seat details/availability “);
System.out.println(” 4. Exit “);
Option=input.nextInt();

if ((Option==1) && (LuxurySeatsSold<4))
{
System.out.println(" Please enter your name ");
CustomerName[LuxurySeatsSold]=input.next();
System.out.println(" ");
System.out.println(" Thank you for your luxury seat order " + CustomerName[LuxurySeatsSold] + " You have seat number " + (LuxurySeatsSold + 1));
LuxurySeatsSold++;
LuxurySeatsSold=TotalLuxurySeats -1;
}
if ((Option==2) && ExectutiveSeatsSold<4)
{
System.out.println(" Please enter your name ");
CustomerName[ExectutiveSeatsSold]=input.next();
System.out.println(" ");
System.out.println(" Thank you for your exectutive seat order " + CustomerName[ExectutiveSeatsSold] + " You have seat number " + (ExectutiveSeatsSold+ 1));
ExectutiveSeatsSold++;
ExectutiveSeatsSold=TotalExecutiveSeats -1;
}
if (Option==3)
{
System.out.println(" Here is a report of available/unavailable seats on the aircraft ");
System.out.println("");

System.out.println(" Luxury seats ");
while (CustomerName[i]!=null)
for (int i=0;j<4;i++) //can not find symbol-variable i".//

{
if(customerName[i]==null) break;
System.out.println("Seat Number: " + i + "Name: " + customerName[i]);

}
System.out.println("Executive Seats");
while(CustomerName[k]!=null)
for( k=4;l<10;k++)
{
if(customerName[k]==null) break;

System.out.println("Seat Number : " + k + "Name: " + customerName[i]); }

}
}
if (Option==4)
{
System.out.println("");
System.out.println("");
System.out.println("");
System.out.println("");
System.out.println("");
System.out.println("");
System.out.println("");
System.out.println("");
System.out.println("");
System.out.println("");
System.out.println("");
System.out.println("");
System.out.println("");
System.out.println("");
System.out.println("=============");
System.out.println(" Good bye,thanks for looking!");
System.out.println("=========");

}
}
}

I realise that i is currently out of scope, however how can I adjust this so that is is?

Thanks in advance for any help,

Ben
JeffKan1- The following line is highlighted after the compile: while (CustomerName[i]!=null) it's still confussing me!

Best answer:

Here’s the ‘i’ it’s talking about:
while (CustomerName[i]!=null)

The variable i only exists in the for loops, which that line of code is not inside.

By Barcode Scanner 3 comments

java die rolling code?

January 30th, 2009 at 01:05pm Under Forum

java die rolling code?
import java.util.Scanner;

public class Die
{
public Die(int s)
{
sides = s;
generator = new Random();
}
public int cast()
{
return 1+generator.nextInt(sides);
}
private Random generator;
private int sides;
}

class DieSimulator
{
public static void main(string[] args)
{
Die d=new Die(6);
final int TRIES = 10;
for (int i= 1; i<=TRIES; i++)
{
int n = d.cast();
System.out.print(n+" ");
}
System.out.println();
}
}
when i compile it cant find symbols our something
thank you for any help you can give me

Best answer:

public class Die
{
public Die(int s)
{
sides = s;
generator = new Random();
}
public int cast()
{
return 1+generator.nextInt(sides);
}
private Random generator;
private int sides;
THESE TWO LINES SHOW BE ON TOP

public class Die
{
private Random generator;
private int sides;
public Die(int s)
{
sides = s;
generator = new Random();
}
public int cast()
{
return 1+generator.nextInt(sides); is this correct??
}

By Barcode Scanner 1 comment

Java – Sorting a pair of Strings AND doubles, by doubles…?

December 21st, 2008 at 05:51am Under Forum

Java – Sorting a pair of Strings AND doubles, by doubles…?
So I’m given this problem, and could use some help. There is a file that consists of a sequence r1 s1 r2 s2 r3 s3 …, where ri is a real number of type double, and si is a String of symbols (that does not include empty spaces in the string its self). If one constructs a pair out of elements ri and si, for each index i, and then sorts these pairs in such a way that first elements of the pairs will be in monotonically increasing order, then the second components of the pairs, separated by empty spaces, form a text to create a paragraph.

So the first thing I have to do is scan the file, and make pairs out of the ri and si, but then I have to sort them by the doubles, ri. Once sorted, I have to print out the text, just the si.

The first few lines of this file look like this:
.3670360229982874 be 2.1758656832275705 of 7.558754198292892 in 7.414471768174697 behind 1.8838317339124944 know 1.20303007486753 hearing 7.734195638483449 and 7.1822892709521495 carried 6.53492389798644 other, 7.237144368426898 of
(note, the text above is all on one line. The word wrap is just breaking it up)

Right now I only have
Scanner sc = new Scanner(new File(“file.input”));

Any ideas would be appreciated! If you have time you can just write the whole program. Thank you! :)

Best answer:

import java.util.Scanner;
import java.io.File;

class sort
{
public static void main(String[]args) throws Exception
{
Scanner sc = new Scanner(new File(“blah.txt”));
obj[] list = new obj[20];

int count=0;
while(sc.hasNext())
{
list[count] = new obj(sc.nextDouble(), sc.next());
count++;
}
sort(list);

for(int i=0;i System.out.println(list[i].getS());

}

public static class obj
{
private double d;
public String s;
public obj(double dd, String ss)
{
d = dd;
s = ss;
}
double getD()
{
return d;
}
String getS()
{
return s;
}
}
static void sort(obj a[])
{
for (int i = a.length; --i>=0; )
{
boolean flipped = false;
for (int j = 0; j {
if (a[j].getD() > a[j+1].getD())
{
obj T = a[j];
a[j] = a[j+1];
a[j+1] = T;
flipped = true;
}
}
if (!flipped)
{ return; }
}
}
}

I didn’t create a test file, but I believe the code should work.

By Barcode Scanner Add comment

Previous Posts


Forum