Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: i dont understand whats wrong with my code, please fix it.

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default i dont understand whats wrong with my code, please fix it.

    im fairly new to java and this is my first programming assignment, our objective is to implement 3 students grades in two different classes (Student, grades) and find the average.


    Specifications: In the new implementation, the two classes, Student and Grades, we are to implement the following methods:

    Student class:
    public class Student - Defines a student with a full name and a complete set of grades:

    public void setup() - Sets all attributes of the student (name and grades).
    private void setName() - Sets the name of the student.
    private void setGrades() - Sets all the grades for the student.
    public void display() - Displays the complete information on the student.
    public double overallGrade() - Returns the overall grade of the student.

    Grades class:
    public class Grades - Defines a complete set of grades received by a student.

    public void setup() - Sets the complete set of grades
    public void display() - Displays the complete set of grades
    public double average() - Returns the average of the complete set of grades (i.e., it returns a number between 0.0 and 100.0).


    Here is my program:


    public class Program01 {

    public static void main(String[] args)
    {
    Student bob, john, matt;
    Grades grades;

    grades = new Grades();

    double bobgrade, johngrade, mattgrade;

    bob = new Student();
    john = new Student();
    matt = new Student();

    bob.setup();
    john.setup();
    matt.setup();

    bob.display();
    john.display();
    matt.display();

    bobgrade = bob.overallGrade();
    johngrade = john.overallGrade();
    mattgrade = matt.overallGrade();

    grades.average(bobgrade, johngrade, mattgrade);

    System.out.println("The overall grade for the class is: " + grades.theSectionAverage);
    }
    }



    public class Student {
    Grades grades;
    String fullName, firstName, lastName, name;
    int studentProgramGrade, studentExamGrade;

    public void setup(){
    setName();
    setGrades();
    }

    public void setName()
    {

    System.out.print("Please, enter the student's name in the form of Doe, John or Smith, Jane:");
    fullName = Keyboard.readString();

    firstName = fullName.substring(fullName.indexOf(" ") + 1, fullName.length());
    lastName = fullName.substring(0, fullName.indexOf(","));


    name = firstName + " " + lastName;
    }

    public void setGrades()
    {
    studentExamGrade = grades.setupExam(name);
    studentProgramGrade = grades.setupProgram(name);
    }

    public void display()
    {
    System.out.println(name + " " + grades.display());
    }

    public double overallGrade()
    {
    final double PROGRAM_WEIGHT = 0.40;
    final double EXAM_WEIGHT = 1 - PROGRAM_WEIGHT;

    double theOverallGrade;

    theOverallGrade = studentProgramGrade * PROGRAM_WEIGHT + studentExamGrade * EXAM_WEIGHT;

    return theOverallGrade;
    }

    }



    public class Grades {
    int programGrade, examGrade;
    double theSectionAverage;

    public int setupExam(String studentname)
    {
    System.out.print("Please, enter the exam grade for " + studentname + ":");
    examGrade = Keyboard.readInt();

    return examGrade;
    }

    public int setupProgram(String studentname)
    {
    Scanner keyboard = new Scanner(System.in);

    System.out.print("Please, enter the program grade for " + studentname + ":");
    programGrade = Keyboard.readInt();

    return programGrade;
    }

    public String display()
    {
    return programGrade + " " + examGrade;
    }

    public double average(double bobgrade, double johngrade, double mattgrade)
    {
    theSectionAverage = bobgrade + johngrade + mattgrade / 3;

    return theSectionAverage;
    }
    }


    when i run my program=, it gives me the following error:

    Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    Keyboard cannot be resolved

    at Student.setName(Student.java:16)
    at Student.setup(Student.java:8)
    at Program01.main(Program01.java:17)



    any help would be greatly appreciated, like i said, im new to java.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: i dont understand whats wrong with my code, please fix it.

    The compiler is telling you that it doesn't know what the object 'Keyboard' is in the Student.setName() method. Since Keyboard was declared and initialized in the setupProgram() method, that is the limit (or scope) of its visibility to the rest of your classes. Since each class is in its own file (and that's fine) each class that requires a Scanner object may have to create its own.

Similar Threads

  1. whats wrong with code, please help me understand thanks
    By guled in forum What's Wrong With My Code?
    Replies: 7
    Last Post: September 22nd, 2013, 12:45 PM
  2. java 2d array is not looping right. i dont understand whyy and how to fix it
    By hwoarang69 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 25th, 2013, 07:13 AM
  3. Problem with import java.util.*; dont know whats wrong
    By TheLeader in forum What's Wrong With My Code?
    Replies: 4
    Last Post: August 3rd, 2012, 12:33 PM
  4. My code has error when its run....I dont understand what's wrong of it.
    By jacky@~ in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 11th, 2011, 07:48 AM
  5. Can't seem to understand whats wrong with my code! Help!!
    By aquir in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 2nd, 2011, 12:06 PM