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 8 of 8

Thread: Simple Question (About Resetting a Class' Fields)

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Simple Question (About Resetting a Class' Fields)

    I am very Java illiterate and I need to know how would i set all the variables in the first class to null or 0 to make a new method to clear everything. Thanks so much


    import java.util.Arrays;
    import java.util.Scanner;

    public class studentMethods
    {


    private double totalScore;
    private int count;
    private String name;
    Scanner in = new Scanner(System.in);
    /*


    System.out.println("Please enter your name: ");
    in.nextString = name;


    */
    public void setName()
    {


    System.out.println("Please enter your name: ");

    String name = in.nextLine();


    }



    public void addQuiz()
    {
    System.out.println();
    System.out.print("How many grades will be entered?");
    while(!in.hasNextInt())
    {
    System.out.print("Please enter an integer: ");
    in.next();
    }
    int numberOfGrades = in.nextInt();
    for(int i = 1; i <= numberOfGrades; i++)
    {
    System.out.println("Enter score "+ i +": ");
    while(!in.hasNextDouble())
    {
    System.out.print("Please enter a number: ");
    in.next();
    }
    double score = in.nextDouble();
    totalScore = totalScore + score;
    count++;
    }
    }


    public void getTotalScore()
    {

    }
    /*
    the getAverageScore method needs to be able to divide the totalScore by the number of
    grades. It must then PRINT the result in the format "[student's name]'s grade average is [averageScore]"
    */
    public void getAverageScore()
    {

    double averageScore = totalScore / count;
    System.out.printf(name + "'s grade average is %.2f\n", averageScore);


    public void clearEverything()
    {

    }

    public char promptContinue()
    {
    System.out.println("New Student?");
    char contin = in.next().charAt(0);

    return contin;
    }
    }







    public class Student
    {
    Scanner in = new Scanner(System.in);

    public static void main(String[]args)
    {
    studentMethods grade = new studentMethods();
    char contin = 'y';
    while(contin == 'y'||contin == 'Y')
    {
    grade.setName();
    grade.addQuiz();
    grade.getTotalScore();
    grade.getAverageScore();
    contin = grade.promptContinue();
    grade.clearEverything();
    }

    }

    }


  2. #2
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default re: Simple Question (About Resetting a Class' Fields)

    1. Directly equate it to null or zero if it is public.
    2. provide a setter for each variable and then set a value for it.
    -setter method must be:
    -return type is void
    -access modifier is public
    -parameter should be same as what your going to set
    -name must start with set (lowercase) followed by name of variable (first letter must be in Uppercase)
    ex:
    private String myName;

    public void setMyName(String myName) {
    this.myName = myName;
    }

    -as you noticed, I have two variables with exactly same name, myName (instance variable) and myName(local variable a parameter)
    to use the instance variable just put this keyword and dot( . ) operator. when not using this keyword, it means that the
    variable you are accessing is the local not the instance.

    3. just make/create another instance of that object using new keyword.

    note: for integer, double and other primitive type (that holds numbers), the default value of them is zero unless it is declared as local variable.
    it is not an error but the naming convention of class in java is always starts with upper case letter.

  3. The Following User Says Thank You to dicdic For This Useful Post:

    dpapi95 (March 31st, 2014)

  4. #3
    Junior Member
    Join Date
    Mar 2014
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default re: Simple Question (About Resetting a Class' Fields)

    I still do not understand . I am way less java informed than you think.

  5. #4
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default re: Simple Question (About Resetting a Class' Fields)

    Which of my explanation you don't understand?
    Please visit this site http://docs.oracle.com/javase/tutorial/java/concepts/
    make sure to read the What is an Object, What is a Class and What is Inheritance in the link I've provided

  6. #5
    Junior Member
    Join Date
    Mar 2014
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default re: Simple Question (About Resetting a Class' Fields)

    can you show me an example of you setting one of my variables to null and 0

  7. #6
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default re: Simple Question (About Resetting a Class' Fields)

    Quote Originally Posted by dpapi95 View Post
    can you show me an example of you setting one of my variables to null and 0
    Okay. just a simple example:
    public class Me {
          private String myName; // default value is "null" not null
          private int age; // default value is 0
     
          // a setter for myName
          public void setMyName(String myName) {
                this.myName = myName;
                /* sets the global variable myName equal to the parameter myName */
          }
     
          // a getter to get the value of myName
          /* access modifier should be public
              return type should be the same of Object type/primitive type of the variable
              method's name should start with get (lower case) followed by name of variable (first letter should be in upper case)
              no parameter should be accepted*/
         public String getMyName() {
              return myName; // returning the value of myName
         }
     
         // setter for age
         public void setAge(int age) {
              this.age = age;
         }
     
         // getter for age
         public int getAge() {
              return age;
         }
    }

    now let us test it using a main method

    public class Test {
     
          // main method
          public static void main(String[] args) {
                 // create an object of Me
                 Me me = new Me(); // me is an object reference variable
                 // next I'll try to print it
                 System.out.println("Name " + me.getMyName());
                 /* to access methods of Me class, just use the Object reference variable we declared above and use dot ( . ) operator before the name of method */
                 System.out.println("Age " + me.getAge());
                 // see what will be the output of the above prins
                 // next, I'll set their value.
                 me.setMyName("Monkey D Luffy");
                 me.setAge(20);
                 // then print it again
                 System.out.println("Name " + me.getMyName());
                 System.out.println("Age " + me.getAge());
          }
    }
    Hope you understand what the above codes do.

    try to compile my example, I just created the code here so I'm not sure if it has compilation error, if it has compilation error, just paste the error here and I'll fix it.

  8. The Following User Says Thank You to dicdic For This Useful Post:

    Zavael (April 1st, 2014)

  9. #7
    Junior Member
    Join Date
    Mar 2014
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default re: Simple Question (About Resetting a Class' Fields)

    Thanks alot

  10. #8
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,066
    My Mood
    Mellow
    Thanks
    184
    Thanked 652 Times in 639 Posts

    Default re: Simple Question (About Resetting a Class' Fields)

    Please give your threads better titles and post your code correctly.

Similar Threads

  1. Re: Resetting Accumulator after first itterneration
    By morrism35 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 2nd, 2013, 06:22 PM
  2. Very simple question (nextDouble- Random class)
    By TSSF44 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 27th, 2013, 10:07 PM
  3. can't compare class fields
    By r0x in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 27th, 2011, 07:06 AM
  4. Cannot set class fields using textInput
    By javascrub in forum What's Wrong With My Code?
    Replies: 0
    Last Post: August 26th, 2010, 04:04 AM
  5. not so simple, simple swing question box
    By wolfgar in forum AWT / Java Swing
    Replies: 2
    Last Post: November 20th, 2009, 02:47 AM