Web Analytics Made Easy -
StatCounter java interface and implementing it? - CodingForum

Announcement

Collapse
No announcement yet.

java interface and implementing it?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • java interface and implementing it?

    In an attempt to get us to better understand object oriented programing I am supposed to use an interface GradeMapper. ClassicRubric and TriageRubric are the different grading scales that i'm using to test this ability. I managed to get those 3 classes working without errors. However once i get to the driver Class I run into a lot of problems.

    Code:
    public class ClassicRubric implements GradeMapper{
        
        public String toLetter(double percent){
            if (percent >= .9)return "A";
            
            else if (percent >= .8)return "B";
            
            else if (percent >= .7)return "C";
            
            else if (percent >= .6)return "D";
        
            else return "F";
        }
    
    }


    Code:
    public class TriageRubric implements GradeMapper{
        
    
        
        public String toLetter (double percent){
            
            
            if (percent > 17.0/18.0){
                return "A";}
            
            else if ((percent >8.0/9.0)){
               return "A-";}
          
            
            else if(percent>5.0/6.0){
               return "B+";
            }
            
            else if(percent>7.0/9.0){
                return "B";
            }
            
            else if(percent>13.0/18.0){
                return "B-";
            }
            
            else if(percent>2.0/3.0){
               return "C+";
            }
            
            else if(percent>3.0/5.0){
               return "C";
            }
            
            else if((percent>8.0/15.0)){
               return "C-";
            }
            
            else if((percent>7.0/15.0)){
                return "D+";
            }
            
            else if(percent>2.0/5.0){
                return "D";
            }
            
            else if((percent>3.0)){
                return "D-";
            }
            
            else {
                return "F";
            }
            
        }
            
    }

    Code:
    package edu.bsu.cs120;
    
    public interface GradeMapper {
        
        public String toLetter(double percent);
    
    }
    Code:
    import java.util.Scanner;
    
    public class Driver {
        
        public static void main{
            
            double pointsEarned;
            double totalPoints;
            int gradingType;
            double percent;
        
            
            System.out.println("input total points");
                
            Scanner input = new Scanner(System.in);
            
            totalPoints = input.nextInt();
            
            System.out.println("input the number of points you earned");
            
            pointsEarned = input.nextInt();
            
            System.out.println("For triage type 1 for classic type 2");
            
            gradingType = input.nextInt();
            
            percent = pointsEarned/totalPoints;
            
            if (gradingType = 1){
                
                new TriageRubric(GradeMapper(percent));
            }
            
            if (gradingType = 0){
                new ClassicRubric(GradeMapper(percent));
            }
            
            
            
        }
    
    }
    the bolded places are where i get errors. How do i make it so that if the user enters 1 then he gets the TriageRubric that uses the "percent" variable? and if he enters 0 he gets the ClassicRubric that uses the "percent" variable.

    also how do i tell it to print out the result to the screen?

    thanks for the help

  • #2
    You're using the assignment operator in your if statments, you should be using the comparison operator:
    gradingType == 1

    These statements here don't make sense to me:
    new TriageRubric(GradeMapper(percent));

    What are you trying to do there? Trying to create a new instance of TriageRubric? If so, I see a few things:
    1. The syntax is off to create a new instance
    2. I don't see any constructor accepting arguments
    3. You cannot make an instance of an interface
    Good luck;
    Last edited by Brandoe85; Oct 8, 2006, 03:58 PM.
    -Brando
    Why using tables for eating is stupid!

    Comment


    • #3
      I'm trying to make it so that when you get the "percent" and which type of grading scale they want (either TriageRubric or ClassicRubrice) that it prints out the line of the right grade. Though i'm not really sure how to do that.

      Comment


      • #4
        instead of:
        Code:
        if (gradingType = 0){
                    new ClassicRubric(GradeMapper(percent));
                }
        try something like
        Code:
        if (gradingType ==0)
        {
            ClassicRubric x = new ClassicRubric();
            System.out.println(x.toLetter(percent));
        }
        notice the "==" instead of "=". this is a common mistake and easy to overlook when trying to find your errors. also notice this line: ClassicRubric x = new ClassicRubric();........make sure you understand how classes are designed and work together, especially constructors, this makes use of the default constructor since you did not specify one in your class. x is defined as an instance of ClassicRubric, and you call the toLetter() method on the object, passing it the percent that you calculated.

        Comment

        Working...
        X