java object making


public class Assignment
{
	private String asName, letter;
	private int asNum, points, max;
	private double score;

	public Assignment()
	{
		asName = "  ";
		asNum = 0;
		max = 0;
	}
	public void setAsName(String newAsName)
	{
		asName = newasName;
	}
	public void setAsNum(int newAsNum)
	{
		asNum = newasNum;
	}
	public void setPoints(int newPoints)
	{
		points = newPoints; 
	}
	public void setMax(int newMax)
	{
		max = newMax;
	}
	public String getasName()
	{
		return asName;
	}
	public int getMax()
	{
		return max;
	}
	public int getasNum()
	{
		return asNum;
	}
	public int getPoints()
	{
		return points;
	}
	public double getScore()
	{
		score = (points / max) * 10;
		return score;
	}
	public String getLetter()
	{
		switch(score)
		{
		case 10.0:
		case 9.0:
			letter = "A";
			break;
		case 8.0:
			letter = "B";
			break;
		case 7.0:
			letter = "C";
			break;
		case 6.0:
			letter = "D";
			break;
		default:
			letter = "F";
			break;
		}
		return letter;
	}
		

	public void writeOutput()
	{
		System.out.println("assignment title: " + asName);
		System.out.println("number: " + asNum);
		System.out.println("points earned: " + points);
		System.out.println("possible total points: " + max);
		System.out.println("Your grade is : " + letter);
	}
}



import java.util.Scanner;

public class gradeBook7{
	
	public static void main(String[] args) 
	{
		Scanner keyboard = new Scanner(System.in);
		
		char grade;
		String courseName;
		int courseSection, courseNumber;
		int score;
		boolean exit = false;
		student s1 = new student();
		student s2 = new student();
		student s3 = new student();
		student s4 = new student();
		student s5 = new student();
		Assignment a1 = new Assignment();
		while (!exit) {
			System.out.println("Enter \'c\' to input course info, \'n\' for student, \'g\' for grades, \'a\' for assignments, \'q\' to exit, ");
			String response = keyboard.next();
			
			if (response.equalsIgnoreCase("c")) {
				System.out.println("Enter the course name");
				courseName = keyboard.next();
				System.out.println("Enter the course section");
				courseSection = keyboard.nextInt();
				System.out.println("Enter the course number");
				courseNumber = keyboard.nextInt();                   
				System.out.println("The course info is " + courseName + " " + courseSection + " "
					+ courseNumber);
			 }
			else if (response.equalsIgnoreCase("n")) {
				System.out.println("Enter the student 1's ID");
				s1.studentID = keyboard.nextInt();
				keyboard.nextLine();
				
				System.out.println("Please enter student 1's first name:");
				s1.firstName = keyboard.nextLine();
				
				System.out.println("Please enter student 1's last name:");
				s1.lastName = keyboard.nextLine();
				
				System.out.println("Please enter student 2's ID");
				s2.studentID = keyboard.nextInt();
				keyboard.nextLine();
				
				System.out.println("Please enter student 2's first name");
				s2.firstName = keyboard.nextLine();

				System.out.println("Please enter student 2's last name");
				s2.lastName = keyboard.nextLine();
				
				System.out.println("Please enter student 3's ID");
				s3.studentID = keyboard.nextInt();
				keyboard.nextLine();
				
				System.out.println("Please enter student 3's first name");
				s3.firstName = keyboard.nextLine();
				
				System.out.println("Please enter student 3's last name");
				s3.lastName = keyboard.nextLine();
				
				System.out.println("Please enter student 4's ID");
				s4.studentID = keyboard.nextInt();
				keyboard.nextLine();
				
				System.out.println("Please enter student 4's first name");
				s4.firstName = keyboard.nextLine();
				
				System.out.println("Please enter student 4's last name");
				s4.lastName = keyboard.nextLine();
				
				System.out.println("Please enter student 5's ID");
				s5.studentID = keyboard.nextInt();
				keyboard.nextLine();
				
				System.out.println("Please enter student 5's first name");
				s5.firstName = keyboard.nextLine();
				
				System.out.println("Please enter student 5's last name");
				s5.lastName = keyboard.nextLine();
				
				System.out.print("You have entered: ");
				System.out.println(s1.toString());
				System.out.println(s2.toString());
				System.out.println(s3.toString());
				System.out.println(s4.toString());
				System.out.println(s5.toString());
			}
			
			else if (response.equalsIgnoreCase("a")) {
			System.out.println("enter assignment name");
			asName = keyboard.nextLine();
			a1.setAsName(asName);
			System.out.println("enter the assignment number");
			asNum = keyboard.nextInt();
			a1.setAsNum(num);
			System.out.println("enter the total point value");
			max = keyboard.nextInt();
			a1.setMax(max);
			System.out.println("enter your score on the assignment");
			points = keyboard.nextInt();
			a1.setPoints(points);
			
			a1.writeOutput();	
			}
			
			else if (response.equalsIgnoreCase("g")) {
				System.out.println("enter the score");
				score = keyboard.nextInt();
				
				switch (score)
				{
				case 100:
				case 99:
				case 98:
				case 97:
				case 96:
				case 95:
				case 94:
				case 93:
				case 92:
				case 91:
				case 90:
					grade = 'A';
					System.out.println("score = " + score);
					System.out.println("grade = " + grade);
				break;
				case 89:
				case 88: 
				case 87:
				case 86:
				case 85:
				case 84:
				case 83:
				case 82:
				case 81:
				case 80:
					grade = 'B';
					System.out.println("score = " + score);
					System.out.println("grade = " + grade);
					break;
				case 79:
				case 78:
				case 77:
				case 76:
				case 75:
				case 74:
				case 73:
				case 72:
				case 71:
				case 70:
					grade = 'C';
					System.out.println("score = " + score);
					System.out.println("grade = " + grade);
					break;
				case 69:
				case 68:
				case 67:
				case 66:
				case 65:
				case 64:
				case 63:
				case 62:
				case 61:
				case 60:
					grade = 'D';
					System.out.println("score = " + score);
					System.out.println("grade = " + grade);
					break;
				case 59:
				case 58:
				case 57:
				case 56:
				case 55:
				case 54:
				case 53:
				case 52:
				case 51:
				case 50:
				case 49:
				case 48:
				case 47:
				case 46:
				case 45:
				case 44:
				case 43:
				case 42:
				case 41:
				case 40:
				case 39:
				case 38:
				case 37:
				case 36:
				case 35:
				case 34:
				case 33:
				case 32:
				case 31:
				case 30:
				case 29:
				case 28:
				case 27:
				case 26:
				case 25:
				case 24:
				case 23:
				case 22:
				case 21:
				case 20:
				case 19:
				case 18:
				case 17:
				case 16:
				case 15:
				case 14:
				case 13:
				case 12:
				case 11:
				case 10:
				case 9:
				case 8:
				case 7:
				case 6:
				case 5:
				case 4:
				case 3:
				case 2:
				case 1:
				case 0:
					grade = 'F';
					System.out.println("score = " + score);
					System.out.println("grade = " + grade);
					break;
				default:
					System.out.println("You have entered a non-valid score.");
					break;
				}
			}
			else if (response.equalsIgnoreCase("q")) {
					exit = true;
			}
		} System.out.println("Goodbye");
	}
}


okay, i’m frustrated. This code will not compile mainly because it cannot find the variable for asName, asNum, max, and points. It can’t find the variables and rejects the set methods i have. I don’t understand why since this theoretically should be correct. as for my grade variable for the Assignment class, it’s giving me problems about having a double variable instead of an int variable. what the heck?

asName only exists in your Assignment object.

You’re trying to assign something to it “asName = keyboard.nextLine();” which wont’ work. It doesn’t exist in the gradeBook7 class.

Your line “a1.setAsName(asName);” might work if you change it to a1.setAsName(keyboard.nextLine());

Remember that your variables are local to your object. That’s kind of the point. You access them with the get/set. You don’t get to play with them otherwise.

You could also create a temporary “asName” within your gradeBook7 class to use to capture input and feed to setAsName()

By the way, you’re inconsistent with the names of your get/set. You are using “setAsName” but “getasName”. That’s going to be really confusing!

Plus your Assignment class won’t compile because of a few typos.

In the setAsNum and setAsName methods, the variable declared in the method signature is

newAsName

But then you attempt to reference it as

newasName

Note the lower case a, java is case sensitive.



public class Assignment
{
	public String asName, letter;
	public int asNum, points, max, value;
	
	public Assignment()
	{
		asName = "  ";
		asNum = 0;
		max = 0;
	}
	public void setAsName(String newAsName)
	{
		asName = newAsName;
	}
	public void setAsNum(int newAsNum)
	{
		asNum = newAsNum;
	}
	public void setPoints(int newPoints)
	{
		points = newPoints; 
	}
	public void setMax(int newMax)
	{
		max = newMax;
	}
	public String getAsName()
	{
		return asName;
	}
	public int getMax()
	{
		return max;
	}
	public int getAsNum()
	{
		return asNum;
	}
	public int getPoints()
	{
		return points;
	}
	public double getValue()
	{
		value = (points / max) * 10;
		return value;
	}
	public String getLetter()
	{
		switch(value)
		{
		case 10:
		case 9:
			letter = "A";
			break;
		case 8:
			letter = "B";
			break;
		case 7:
			letter = "C";
			break;
		case 6:
			letter = "D";
			break;
		default:
			letter = "F";
			break;
		}
		return letter;
	}
		

	public void writeOutput()
	{
		System.out.println("assignment title: " + asName);
		System.out.println("number: " + asNum);
		System.out.println("points earned: " + points);
		System.out.println("possible total points: " + max);
		System.out.println("Your grade is : " + letter);
	}
}




import java.util.Scanner;

public class gradeBook7{
	
	public static void main(String[] args) 
	{
		Scanner keyboard = new Scanner(System.in);
		
		char grade;
		String courseName;
		int courseSection, courseNumber;
		int score;
		boolean exit = false;
		student s1 = new student();
		student s2 = new student();
		student s3 = new student();
		student s4 = new student();
		student s5 = new student();
		Assignment a1 = new Assignment();
		while (!exit) {
			System.out.println("Enter \'c\' to input course info, \'n\' for student, \'g\' for grades, \'a\' for assignments, \'q\' to exit, ");
			String response = keyboard.next();
			
			if (response.equalsIgnoreCase("c")) {
				System.out.println("Enter the course name");
				courseName = keyboard.next();
				System.out.println("Enter the course section");
				courseSection = keyboard.nextInt();
				System.out.println("Enter the course number");
				courseNumber = keyboard.nextInt();                   
				System.out.println("The course info is " + courseName + " " + courseSection + " "
					+ courseNumber);
			 }
			else if (response.equalsIgnoreCase("n")) {
				System.out.println("Enter the student 1's ID");
				s1.studentID = keyboard.nextInt();
				keyboard.nextLine();
				
				System.out.println("Please enter student 1's first name:");
				s1.firstName = keyboard.nextLine();
				
				System.out.println("Please enter student 1's last name:");
				s1.lastName = keyboard.nextLine();
				
				System.out.println("Please enter student 2's ID");
				s2.studentID = keyboard.nextInt();
				keyboard.nextLine();
				
				System.out.println("Please enter student 2's first name");
				s2.firstName = keyboard.nextLine();

				System.out.println("Please enter student 2's last name");
				s2.lastName = keyboard.nextLine();
				
				System.out.println("Please enter student 3's ID");
				s3.studentID = keyboard.nextInt();
				keyboard.nextLine();
				
				System.out.println("Please enter student 3's first name");
				s3.firstName = keyboard.nextLine();
				
				System.out.println("Please enter student 3's last name");
				s3.lastName = keyboard.nextLine();
				
				System.out.println("Please enter student 4's ID");
				s4.studentID = keyboard.nextInt();
				keyboard.nextLine();
				
				System.out.println("Please enter student 4's first name");
				s4.firstName = keyboard.nextLine();
				
				System.out.println("Please enter student 4's last name");
				s4.lastName = keyboard.nextLine();
				
				System.out.println("Please enter student 5's ID");
				s5.studentID = keyboard.nextInt();
				keyboard.nextLine();
				
				System.out.println("Please enter student 5's first name");
				s5.firstName = keyboard.nextLine();
				
				System.out.println("Please enter student 5's last name");
				s5.lastName = keyboard.nextLine();
				
				System.out.print("You have entered: ");
				System.out.println(s1.toString());
				System.out.println(s2.toString());
				System.out.println(s3.toString());
				System.out.println(s4.toString());
				System.out.println(s5.toString());
			}
			
			else if (response.equalsIgnoreCase("a")) {
			System.out.println("enter assignment name");
			a1.asName = keyboard.nextLine();
			keyboard.nextLine();
			System.out.println("enter the assignment number");
			a1.asNum = keyboard.nextInt();
			System.out.println("enter the total point value");
			a1.max = keyboard.nextInt();
			System.out.println("enter your score on the assignment");
			a1.points = keyboard.nextInt();
			
			a1.writeOutput();	
			}
			
			else if (response.equalsIgnoreCase("g")) {
				System.out.println("enter the score");
				score = keyboard.nextInt();
				
				switch (score)
				{
				case 100:
				case 99:
				case 98:
				case 97:
				case 96:
				case 95:
				case 94:
				case 93:
				case 92:
				case 91:
				case 90:
					grade = 'A';
					System.out.println("score = " + score);
					System.out.println("grade = " + grade);
				break;
				case 89:
				case 88: 
				case 87:
				case 86:
				case 85:
				case 84:
				case 83:
				case 82:
				case 81:
				case 80:
					grade = 'B';
					System.out.println("score = " + score);
					System.out.println("grade = " + grade);
					break;
				case 79:
				case 78:
				case 77:
				case 76:
				case 75:
				case 74:
				case 73:
				case 72:
				case 71:
				case 70:
					grade = 'C';
					System.out.println("score = " + score);
					System.out.println("grade = " + grade);
					break;
				case 69:
				case 68:
				case 67:
				case 66:
				case 65:
				case 64:
				case 63:
				case 62:
				case 61:
				case 60:
					grade = 'D';
					System.out.println("score = " + score);
					System.out.println("grade = " + grade);
					break;
				case 59:
				case 58:
				case 57:
				case 56:
				case 55:
				case 54:
				case 53:
				case 52:
				case 51:
				case 50:
				case 49:
				case 48:
				case 47:
				case 46:
				case 45:
				case 44:
				case 43:
				case 42:
				case 41:
				case 40:
				case 39:
				case 38:
				case 37:
				case 36:
				case 35:
				case 34:
				case 33:
				case 32:
				case 31:
				case 30:
				case 29:
				case 28:
				case 27:
				case 26:
				case 25:
				case 24:
				case 23:
				case 22:
				case 21:
				case 20:
				case 19:
				case 18:
				case 17:
				case 16:
				case 15:
				case 14:
				case 13:
				case 12:
				case 11:
				case 10:
				case 9:
				case 8:
				case 7:
				case 6:
				case 5:
				case 4:
				case 3:
				case 2:
				case 1:
				case 0:
					grade = 'F';
					System.out.println("score = " + score);
					System.out.println("grade = " + grade);
					break;
				default:
					System.out.println("You have entered a non-valid score.");
					break;
				}
			}
			else if (response.equalsIgnoreCase("q")) {
					exit = true;
			}
		} System.out.println("Goodbye");
	}
}


ah okay, that helped a bit. regarding the error message, i did fix the accessor and mutator method, but i was still getting a missing variable message for the keyboard lines… unfortunately, i decided to make the variable public since i couldn’t figure out why the private variables were not working. the only question i got is anybody know why i get the word “null” for grade even though there are methods to get a letter grade based on the score?

I’m not sure what the intent of this code is (I assume it’s for a class) but I think you are doing yourself a disservice in reverting to public properties. By bypassing the accessor functions you eliminate most of the advantages of having an Assignment class.

I don’t see anything glaring about the “grade” variable, though I have two recommendations in that section. One, check your input (that’s true of all your inputs – you don’t verify any of them). You ask for a score, but the user can type in anything. What if they type in “a”, or “99.7”. You can’t handle that. You should. Second, the switch statement is probably not the best mechanism to handle that. I’d probably start with an if/elseif sequence myself. (And I’d make score a float, not an int)