AP Computer Science Name:
Test 10/31/2014 Period:
1.
//Write a class Students. Use two instance variables. One should be of data type int and the other one should be of the String class.
// Students has a constructor that takes the grade level as a parameter (9 for Freshman, 10 for Sophomore, 11 for Junior, and 12 for Senior)
// public Students(int gradeLevel)
//If grade Level is not 9 to 12, set it to 9
//And 2 methods
// public String getClassName() Use if / else if /else if ...
// public int getNumberOfStudents() Do NOT use a separate if / else branch for each month. Use Boolean operators (&, ||, !).
// You will have an if then else if then else.
// For Freshman return 900, for Sophomore return 700, for Junior and Senior return 500.
//
//Now write a class StudentsTester which will instantiate a Students object, ask the user for a month number
//and print the name of the class and the number of students for the class.
//If the grade level is 10, the output should look exactly like this
//Sophomore 700
//If the user enters an invalid month number print this error message: "Number must be 9 through 12"
//If the user enters a non-integer, give this error message: "Not an integer. Terminating"
//To be considered correct, you must enter the message exactly as specified.
//In StudentsTester, ask the user for a number 9 to 12 and create a month object with that number.
// Do not use a loop for to implement this class, but use nested if statements.
import java.util.Scanner;
public class MonthPrinter
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Enter a month (9 through 12) " );
//your code here
}
}
2. What will this code print? Ans:______
String school = new String(“Udacity”);
If(school == “Udacity”)
{
System.out.println(“good”);
}
Else
{
System.out.println(“bad”);
}
3. What will be printed if grade is 85? Ans:______
String letterGrade = “F”;
if(grade >= 90){letterGrade = “A”;}
if{grade>=80){letterGrade = “B”;}
if{grade>=70){letterGrade =”C”;}
else{letterGrade =”D”;}
System.out.println(letterGrade);
4. What does this code segment print?
int count = 0;
for(count = 0; count<=10; count=count+3)
{
System.out.print(count + “ “);
}
System.out.println(count);
Ans:______
5//. Complete the code below to print each letter in the //string name followed by a space.
// You must use the length() and substring( , ) method.
//
public class StringTester
{
public static void main(String[] args)
{
String name = "Udacity";
//your code here
}
}