Quantcast
Channel: flounderCode()
Viewing all articles
Browse latest Browse all 10

JAVA records

$
0
0

Records are a simple way to create a single variable that can hold different values. Think of an address. An address has many parts but it is conceptually a single unit. Below is some code that demonstrates creating a record.

//BTG 2.10.14  
/*
 * This is a simple program that creates a small database and stores user entry into values.
 * Records are an easy way to create a single variable that can hold several different values.
 * See below for detailed comments.
 */


import java.util.Scanner;

class Student{
    String name;
    int grade;
    double average;
        
        //toString returns a string representation of the current object
        public String toString(){
            return ("The students name is: " +this.name+ "  The students grade is : " +this.grade+ "  The students averages is: " +this.average);
        }
}



public class aLittleDatabase{
    
    public static void main (String[] args){
    
    Student[] student = new Student[3];  //initialize an array to hold 3 student records
    Scanner keyboard = new Scanner(System.in);
    
        for ( int i=0; i < student.length; i++ ){  
            student[i] = new Student();  //calls Student class
            System.out.print("Enter student "+(i+1)+"'s name:  ");
            student[i].name = keyboard.next();  //.name record is created
            System.out.print("Enter student "+(i+1)+"'s grade:  ");
            student[i].grade = keyboard.nextInt();  //.grade record
            System.out.print("Enter student "+(i+1)+"'s average:  ");
            student[i].average = keyboard.nextDouble();  //.average record    
        }
        
        for ( int i=0; i < student.length; i++ )
        {
            System.out.println( (i+1) + ". " + student[i] );
        }
    
    }
    
}


Viewing all articles
Browse latest Browse all 10

Trending Articles