I am EXTREMELY new to Java and completely lost on the concept of ARRAYS. I need to create a "World" class to display a two dimensional array that is user specified in size, using a constructor that accepts two values [rows] and [columns] with a character (Lets say "P") located in the array. A separate "Driver" class will hold the main method. Other methods (moveUp, moveDown, moveLeft, and moveRight) need to be created to move the character around inside the array. There needs to be a fifth method to display the world array. I currently have the following code, but nothing is working so I got rid of it. even this code itself won't compile - says "identifier expected before the parenthesis of my second println. I have no idea why this won't go any further. This is the only place I have to seek help since the college offers no tutors for Java, youtube videos are extremely vague and use terminology I do not know, library books I've checked out don't show me the actual code needed to perform these tasks, and the class textbook shows code close, but not quite what is needed. and since I'm extremely new to Java, my last questions garnered me the threat of losing the ability to post on this site, so that would take away my only recourse for help if this is also dubbed inferior. I do not know what to do at this point.
import java.util.*;
public class World
{
public static void main(String[] args)
{
System.out.println(array);
}
Scanner input = new Scanner(System.in);
System.out.println("Enter number of row: ");
private int crow = input.nextInt();
System.out.println("Enter number of columns: ");
private int ccol = input.nextInt();
private String[][] array = newString[crow][ccol];
public int displayWorld()
{
}
public int moveUp()
{
}
public int moveDown()
{
}
public int moveLeft()
{
}
public int moveRight()
{
}
}
Compling problems-> use an IDE (eclipse netbeans idea jdeveloper ...) for developing java applications.
Here you have part of one solution, since you are learning, play with this code before implementing the rest of methods.
Regarding java learning, there are plenty of good tutorials by searching on google.
import java.util.*;
public class World{
private static final String P="P";
private String[][] array;
public World(){
Scanner input = new Scanner(System.in);
System.out.println("Enter number of row: ");
int crow = input.nextInt();
System.out.println("Enter number of columns: ");
int ccol = input.nextInt();
array = new String[crow][ccol];
array[0][0]=P;
}
public void displayWorld(){
System.out.println();
for(int i=0;i<array.length;i++){
for (int j=0;j<array[i].length;j++){
System.out.print(array[i][j]+" ");
}
System.out.println();
}
}
public void moveUp(){
}
public void moveDown(){
for(int i=0;i<array.length;i++){
for (int j=0;j<array[i].length;j++){
if ((array[i][j])!=null){
if (i<array.length-1){
array[i][j]=null;
array[i+1][j]=P;
}
return;
}
}
}
}
public void moveLeft(){
}
public void moveRight(){
}
public static void main(String[] args){
World world=new World();
world.displayWorld();
world.moveDown();
world.displayWorld();
}
}
Related
Using LinkedList I want to access the data members of the class StudData. StudData should have an array of object. This code doesn't show errors but doesn't execute successfully either.
import java.util.LinkedList;
import java.util.Scanner;
public class StudData {
public int roll_no;
public String name;
private Scanner sc;
void enter() {
sc = new Scanner(System.in);
System.out.println("enter:");
sc.nextInt(roll_no);
sc.next(name);
}
public static void main(String[] args) {
StudData p= new StudData();
LinkedList <StudData> ll=new LinkedList<StudData>();
for (int i=0; i<20; i++) {
p.enter();
ll.add(p);
}
}
}
The code shared should compile ideally. But there would be a possible exception at:
sc.nextInt(roll_no); // roll_no is 0 by default
Hence this would throw an java.lang.IllegalArgumentException: radix:0. In case you want to take roll_no as an input from user, you can change the code to:
roll_no = sc.nextInt();
It looks to me like you made a mistake (although I am on the train working with my phone)
sc.nextInt(roll_no);
sc.next(name);
Should be:
roll_no = sc.nextInt();
name = sc.next();
The variables can't be set by passing them as arguments, because a String is immutable and an int is a primitive.
OK im kinda new in java and i made this averaging program in one class, but now if i want to like call it from another class, then how do i do it?I tryed some object stuff but its hard to understand for.
this is the code and i want this program to start when i call it.
package Gangsta;
import java.util.Scanner;
public class okidoki {
public static void main(String []args){
Scanner input = new Scanner(System.in);
double average, tests, grades, total = 0;
int cunter = 1, start = 0;
System.out.println("Press 2 to start averaging, or press 1 to end");
start = input.nextInt();
while (cunter<start){
System.out.println("Enter how many tests u have");
tests = input.nextDouble();
System.out.println("Enter tests grades");
int counter = 0;
while (counter<tests){
counter++;
grades = input.nextDouble();
total = grades+total;
}
average = total/tests;
System.out.println(average);
System.out.println("Press 3 to end or 1 to average again");
cunter = input.nextInt();}
}
}
this is the code where i want to execute it
package Gangsta;
public class tuna {
public static void main(String []args){
okidoki okidokiObject = new okidoki();
System.out.println(okidokiObject);
}
}
In java the main method is the main (it's in the name) entry point of a program. That said, there is only one main in your program. Nevertheless if you want to have your code wrapped in another class just do it:
public class MyClass {
public void myFancyMethod() {
Scanner input = new Scanner(System.in);
//....rest of
//....your code
counter = input.nextInt();
}
}
and access it like:
MyClass myClassObject = new MyClass();
myClassObject.myFancyMethod();
You should really start reading (or read them again) the fundamentals of object oriented programming languages, naming conventions etc, because this is something you should understand to make progress in programming.
Object-Oriented Programming Concepts in Java
For now, you can just do this in tuna.java to achieve what you want:
package Gangsta;
public class tuna {
public static void main(String []args){
okidoki okidokiObject = new okidoki();
okidokiObject.main()
}
}
System.out.println(okidokiObject) prints Gangsta.okidoki#659e0bfd because it is the hashcode of your object (Hashcode is something like an ID, See Object toString()). You usually do not want to print objects, but invoke their methods.
If you change your main method in okidoki class to constructor it will work exactly like you wish !
Example:
Example.java
public class Example {
public Example() {
System.out.println("Example class constructed");
}
}
Main.java
public class Main {
public static void main(String[] args) {
System.out.println("Program started.Constructing Example class");
Example exClass = new Example();
System.out.println("Program finished.");
}
}
I apologize if the answer to this question is so obvious I shouldn't even be posting this here but I've already looked up the error compiling the following code results in and found no explanation capable of penetrating my thick, uneducated skull.
What this program is meant to do is get 2 integers from the user and print them, but I have somehow managed to mess up doing just that.
import java.util.Scanner;
public class Exercise2
{
int integerone, integertwo; //putting ''static'' here doesn't solve the problem
static int number=1;
static Scanner kbinput = new Scanner(System.in);
public static void main(String [] args)
{
while (number<3){
System.out.println("Type in integer "+number+":");
if (number<2)
{
int integerone = kbinput.nextInt(); //the integer I can't access
}
number++;
}
int integertwo = kbinput.nextInt();
System.out.println(integerone); //how do I fix this line?
System.out.println(integertwo);
}
}
Explanation or a link to the right literature would be greatly appreciated.
EDIT: I want to use a loop here for the sake of exploring multiple ways of doing what this is meant to do.
Remove the int keyword when using the same variable for the second time. Because when you do that, it is essentially declaring another variable with the same name.
static int integerone, integertwo; // make them static to access in a static context
... // other code
while (number<3){
System.out.println("Type in integer "+number+":");
if (number<2)
{
integerone = kbinput.nextInt(); //no int keyword
}
number++;
}
integertwo = kbinput.nextInt(); // no int keyword
And it needs to be static as well since you're trying to access it in a static context (i.e) the main method.
The other option would be to declare it inside the main() method but before your loop starts so that it'll be accessible throughout the main method(as suggested by "Patricia Shanahan").
public static void main(String [] args) {
int integerone, integertwo; // declare them here without the static
... // rest of the code
}
How about:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner kbinput = new Scanner(System.in);
System.out.println("Type in an integer: ");
int integerone = kbinput.nextInt();
System.out.println("Type another: ");
int integertwo = kbinput.nextInt();
System.out.println(integerone);
System.out.println(integertwo);
}
}
I've been studying Deitel's Book(Java how to program) and I want to solve exercise 6.35. Here's what it asks:
Write a program to assist a student to learn multiplication.Use a Random object to produce two positive integers (one digit each).
The program should show on screen something like this:("How much is 7 times 3")
Then the student should insert the answer and the program controls if the answer is correct or wrong.If it's correct the program continue asking another question,else the program waits until the student answer is correct.For every new question it must be a new method created(this method should be called once when the application starts and when the user answers correct to a question).
How do I do this?
//I have a problem inside the do-while block!
package multiplication;
import java.util.Random;
import java.util.Scanner;
/*Hey again! I've been trying to solve this problem using NetBeans environment
*
*/
public class Ypologismos
{
private int p;
private int a,b;
public Ypologismos(int a,int b,int p)
{
this.a=a;
this.b=b;
this.p=p;
}
public Ypologismos()
{
}
public void Screen()
{
System.out.println("Wrong answer ....please retry");
}
public void askForNumbers()
{
Random r=new Random();
int a,b;
a=r.nextInt(10);
b=r.nextInt(10);
int p;//p=product
p=(a*b);
System.out.println("How much is:"+" "+a+" "+"times"+" "+b+" "+"?");
System.out.println("Please insert your answer!");
Scanner s=new Scanner(System.in);
int ans;//ans=answer
ans=s.nextInt();
do
{
while(ans==p){
System.out.println("Well done!");
askForNumbers();
}
}while(ans!=p);
}
}
//and my main class ...
package multiplication;
public class Main
{
public static void main(String[] args)
{
Ypologismos application=new Ypologismos();
application.askForNumbers();
}
}
Make a terse story book of how to do it.
teach multiplication:
repeat // solving problems
int first number = something random
int second number = something random
int product = first number * second number
repeat
int answer = ask how much is first number times second number
if answer != product
say error!
until answer == product
say solved!
The above is just a first idea, not necessarily following the requirements. But it clears which loop comes in which loop and so on.
Reading your extended question
public class Ypologismos {
/** Entry point to the application. */
public static void main(String[] args) {
Ypologismos application = new Ypologismos();
application.teachMultiplication();
}
private void teachMultiplication() {
while (wantsAProblem()) {
askAProblem();
}
}
private void askAProblem() {
int αλφα = random.nextInt(10);
int βητα = random.nextInt(10);
...
}
}
I'm new to Java, and need help. I have been asked to write a program that rolls dice and determines the chance of the player to get two "1s" on the top face of the dice. I have different functions such as role(), getTopFace() etc. I want to be get what the number on the dice is using these functions, but don't know how to call them in my main function. Here's my code:
import javax.swing.JOptionPane;
import java.util.Random;
public class SnakeEyes {
private final int sides;
private int topFace;
public static void main(String[]args)
{
String numberSides;
int n;
numberSides=JOptionPane.showInputDialog("Please enter the number of sides on the dice:");
n = Integer.parseInt ( numberSides);
int[]die=new int[n];
for (int index=0; index<n;index++)
{
die[index]=index+1;
}
//Here is where I want to get information from my functions and calculate the ods of getting two 1's.
}
public void Die(int n)
{
if(n>0)
{
int sides=n;
topFace=(int)(Math.random()*sides)+1;
}
else{
JOptionPane.showMessageDialog(null, " Die : precondition voliated");
}
}
public int getTopFace(int topFace)
{
return topFace;
}
public int role(int[] die)
{
topFace=(int)(Math.random()*sides)+1;
return topFace;
}
}
Make an object of your class SnakeEyes in your main method, and call the required functions using that object.
Example:
SnakeEyes diceObj = new SnakeEyes();
int topFace = diceObj.role(n,....);
If you want to call this functions from main this functions must be "static", because main its a static function and static function can only call other static functions.
But... this is a very ugly design for a java program, before jumping to write java code you need to understand at least a little about object orientation. For example, why you can't call a non-static function from a static function?, the answer of this question requires knowledge about object orientation and its a knowledge you need if you want to write serious java code.