Welcome to Dream.In.Code
Become a Java Expert!

Join 150,104 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,855 people online right now. Registration is fast and FREE... Join Now!




Help With 2 Programs

 
Reply to this topicStart new topic

Help With 2 Programs

undergroundthry
24 Jun, 2008 - 07:12 PM
Post #1

New D.I.C Head
*

Joined: 24 Jun, 2008
Posts: 10

Hey all! I'm a new member here and a new member to the programming community. I've managed to do a few simple programs, but I do have a problem with 2:

1. This is a program which is supposed to translate a letter grade into a numeric value. The assignment states that a "+" or a "+" will change the letter grade by 0.3 (i.e. B- is equivalent to a 2.7). The constraints are that an F+, F-, and A+ should yield the solid grade (i.e. an A+ should equal the same of an A. An F+/F- is the same value as an F (50). I don't know where to start. I have the base grade programmed, but adding or subtracting 0.3 confuses me.

CODE

import java.util.*;
class Grades
{
  public static void main(String[] args)

  {
   Scanner in=new Scanner(System.in);
   String grade="";
   System.out.println("Enter your letter grade");
   grade=in.nextLine();

   String letter=grade.substring(0,1);
   String symbol=grade.substring(1,2);

   if (letter.equals("A"))
    System.out.print("4.0");
   else if (letter.equals("B"))
    System.out.print("3.0");
   else if (letter.equals("C"))
    System.out.print("2.0");
   else if (letter.equals("D"))
    System.out.print("1.0");
   else if (letter.equals("F"))
    System.out.print("0.0");

     if
   else if (symbol.equals("+"))
    System.out.print("le");
   else if (symbol.equals("-"))
    System.out.print("-0.3");
    }
}


2. This program is for a game of Nim. It's a very simple program compared to those that are posted on the internet. I know that this is the correct code because my teacher approved, however, he wouldn't help me in identifying the errors. This is what my code looks like:

CODE

import java.util.*;
class Nim
{
  public static void main(String[] args)
  {
    Scanner in=new Scanner(System.in);
    Random generator = new Random();
    int marbles = 10 + generator.nextInt(91), person = generator.nextInt(2);
    mode = generator.nextInt(2);
    System.out.println("size" + marbles);
     while (marbles>0)
     {
       if (person == 1)
       {
         System.out.print("Pick");
         marbles = marbles-pick;
         System.out.println(marbles + "remaining");
             }
             else
             {
                 int randPick = 0;
                 if (marbles==1)
                   randPick = 1;
                   else randPick = 1 + generator.nextInt(marbles/2);
                 if (mode==1)
                 {
                     marbles = marbles-randPick;
                     System.out.print(marbles + "remaining");
                 }

                 else {if (marbles==63 || marbles==31 || marbles==15 || marbles==7 || marbles==3)
                      marbles = marbles-randPick;
                      else if (marbles > 63)
                       pick = marbles - 63;
                      else if (marbles > 31)
                       pick = marbles - 31;
                      else if (marbles > 15)
                       pick = marbles - 15;
                      else if (marbles > 7)
                       pick = marbles - 7;
                      else if (marbles > 3)
                       pick = marbles - 3;
                      System.out.print(marbles + "remaining");
                    }
                }
                if (person==0)
                 person = 1;
                else
                 person = 0;
                if (person==1)
                 System.out.println("You win");
                else System.out.println("You lose");
            }
        }
    }


I get 8 errors, which state

CODE

C:\Documents and Settings\Admin\Desktop\Nim.java:9: cannot find symbol
symbol  : variable mode
location: class Nim
    mode = generator.nextInt(2);
    ^
C:\Documents and Settings\Admin\Desktop\Nim.java:16: cannot find symbol
symbol  : variable pick
location: class Nim
         marbles = marbles-pick;
                           ^
C:\Documents and Settings\Admin\Desktop\Nim.java:25: cannot find symbol
symbol  : variable mode
location: class Nim
                 if (mode==1)
                     ^
C:\Documents and Settings\Admin\Desktop\Nim.java:34: cannot find symbol
symbol  : variable pick
location: class Nim
                       pick = marbles - 63;
                       ^
C:\Documents and Settings\Admin\Desktop\Nim.java:36: cannot find symbol
symbol  : variable pick
location: class Nim
                       pick = marbles - 31;
                       ^
C:\Documents and Settings\Admin\Desktop\Nim.java:38: cannot find symbol
symbol  : variable pick
location: class Nim
                       pick = marbles - 15;
                       ^
C:\Documents and Settings\Admin\Desktop\Nim.java:40: cannot find symbol
symbol  : variable pick
location: class Nim
                       pick = marbles - 7;
                       ^
C:\Documents and Settings\Admin\Desktop\Nim.java:42: cannot find symbol
symbol  : variable pick
location: class Nim
                       pick = marbles - 3;
                       ^
8 errors

Tool completed with exit code 1


but I don't know how to fix this.

I'm eagerly awaiting any help I can get! biggrin.gif
User is offlineProfile CardPM
+Quote Post

pbl
RE: Help With 2 Programs
24 Jun, 2008 - 07:47 PM
Post #2

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
First your code does not even compile

CODE

   else if (letter.equals("F"))
    System.out.print("0.0");

     if
   else if (symbol.equals("+"))
    System.out.print("le");


The "if" there does not make sense


CODE

    mode = generator.nextInt(2);

"mode" is not defined.... an int I guess

CODE

    marbles = marbles-pick;


This is not a valid Java statement unless marbles and pick are instance variables


User is offlineProfile CardPM
+Quote Post

undergroundthry
RE: Help With 2 Programs
24 Jun, 2008 - 08:07 PM
Post #3

New D.I.C Head
*

Joined: 24 Jun, 2008
Posts: 10

QUOTE(pbl @ 24 Jun, 2008 - 08:47 PM) *

First your code does not even compile

CODE

   else if (letter.equals("F"))
    System.out.print("0.0");

     if
   else if (symbol.equals("+"))
    System.out.print("le");


The "if" there does not make sense


CODE

    mode = generator.nextInt(2);

"mode" is not defined.... an int I guess

CODE

if
   else if (symbol.equals("+"))
    System.out.print("le");
   else if (symbol.equals("-"))
    System.out.print("-0.3");
    }

    marbles = marbles-pick;


This is not a valid Java statement unless marbles and pick are instance variables


The "if" in the first code is a typo. If I removed
CODE

   String symbol=grade.substring(1,2);


and

CODE

  if
   else if (symbol.equals("+"))
    System.out.print("le");
   else if (symbol.equals("-"))
    System.out.print("-0.3");


the program compiles. However, I don't know how I would subtract a value from "letter".

On the second program, I get error messages where "mode" and "pick" variable are not defined. How would I define them?

User is offlineProfile CardPM
+Quote Post

pbl
RE: Help With 2 Programs
24 Jun, 2008 - 08:13 PM
Post #4

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
I guess you'l have to re-post your current code
User is offlineProfile CardPM
+Quote Post

undergroundthry
RE: Help With 2 Programs
25 Jun, 2008 - 07:02 PM
Post #5

New D.I.C Head
*

Joined: 24 Jun, 2008
Posts: 10

QUOTE(pbl @ 24 Jun, 2008 - 09:13 PM) *

I guess you'l have to re-post your current code


OK so I think I'm getting closer to my objective regarding the first code about grades.

CODE

import java.util.*;
class Grades
{
  public static void main(String[] args)

  {
   Scanner in=new Scanner(System.in);
   double plus = +0.3;
   double minus = -0.3;
   String grade="";
   System.out.println("Enter your letter grade");
   grade=in.nextLine();

   String letter=grade.substring(0,1);
   String symbol=grade.substring(1,2);

   if (letter.equals("A"))
    System.out.print("4.0");
   else if (letter.equals("B"))
    System.out.print("3.0");
   else if (letter.equals("C"))
    System.out.print("2.0");
   else if (letter.equals("D"))
    System.out.print("1.0");
   else if (letter.equals("F"))
    System.out.print("0.0");

{
    if (symbol.equals("-"))
     System.out.print(letter + minus);
    if (symbol.equals("+"))
     System.out.print(letter + plus);
}
    }
}


The problem I'm having with this though is that when I enter a letter grade, say "A", the program doesn't run. However, when I input "B-", the program yields "3.0B-0.3". That's sort of what I want, but I want the numeric value, so if I input "B-", I want the program to yield "2.7".

Can anybody tell me where I went wrong?
User is offlineProfile CardPM
+Quote Post

pbl
RE: Help With 2 Programs
25 Jun, 2008 - 07:19 PM
Post #6

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
Here is your code with some validations added free of charge

java

import java.util.*;
class Grades
{
public static void main(String[] args)

{
Scanner in=new Scanner(System.in);
double plus = +0.3;
double minus = -0.3;

System.out.print("Enter your letter grade: ");
String grade=in.nextLine();

String letter=grade.substring(0,1);

double value;
if (letter.equals("A"))
value = 4.0;
else if (letter.equals("B"))
value = 3.0;
else if (letter.equals("C"))
value = 2.0;
else if (letter.equals("D"))
value = 1.0;
else if (letter.equals("F"))
value = 0.0;
else {
System.out.println("Invalid grade letter entered");
return;
}

// before attempting to get symbol make sure 2 digits were entered
if(grade.length() > 1) {
String symbol = grade.substring(1,2);
if (symbol.equals("-"))
value += minus;
else if (symbol.equals("+"))
value += plus;
else
System.out.println("Invalid symbol entered");
}
System.out.println("Value in old notation: " + value);
}
}




User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 12:46AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month