我正在学习如何使用类和方法,但没有任何东西可以通过我。我已经阅读了教科书中的章节和大量的网页。我正在编写一个Java程序,它将从2个测验,期中考试和期末考试中获得成绩,并吐出所有成绩,最终加权成绩和字母等级。我已经尝试了很多次,但是不能用类来做。我能够编写一个无需实现类的程序,但我真的想知道类。
这就是我没有上课的方式:
import java.util.Scanner;
public class grade {
public static void main(String args[]) {
//quiz method
Scanner keyboard = new Scanner(System.in);
System.out.printf("Please enter the grade of Quiz 1: ");
double a = keyboard.nextInt();
System.out.printf("Please enter the grade of Quiz 2: ");
double b = keyboard.nextInt();
double c = (a + b);
double d = (c / 20);
double quizGrade = (d * 25);
//midterm method
System.out.printf("Please enter the grade of the Midterm: ");
double aa = keyboard.nextInt();
double bb = (aa / 100);
double midtermGrade = (bb * 25);
//final method
System.out.printf("Please Enter the grade of the Final: ");
double aaa = keyboard.nextInt();
double bbb = (aaa / 100);
double finalGrade = (bbb * 50);
double overallGrade = (quizGrade + midtermGrade + finalGrade);
System.out.printf("Score for Quiz 1 is: %f\n", a);
System.out.printf("Score for Quiz 2 is: %f\n", b);
System.out.printf("Score for Midterm is: %f\n", aa);
System.out.printf("Score for Final is: %f\n", aaa);
//figuring letter grade
if (overallGrade >= 90) {
System.out.printf("Your overall grade is: %f\n", overallGrade);
System.out.printf("Your letter grade is: A\n");
}
if (overallGrade >= 80 && overallGrade <= 89) {
System.out.printf("Your overall grade is: %f\n", overallGrade);
System.out.printf("Your letter grade is: B\n");
}
if (overallGrade >= 70 && overallGrade<= 79) {
System.out.printf("Your overall grade is: %f\n", overallGrade);
System.out.printf("Your letter grade is: C\n");
}
if (overallGrade >= 60 && overallGrade<= 69) {
System.out.printf("Your overall grade is: %f\n", overallGrade);
System.out.printf("Your letter grade is: D\n");
}
if (overallGrade < 60) {
System.out.printf("Your overall grade is: %f\n", overallGrade);
System.out.printf("Your letter grade is: F\n");
}
}
}
这就是我用课程尝试的方式......我认为可以说我不知道自己在做什么:
import java.util.Scanner;
public class grade2 {
Scanner keyboard = new Scanner(System.in);
System.out.printf("Please enter the grade of Quiz 1: ");
double grade1 = keyboard.nextInt();
System.out.printf("Please enter the grade of Quiz 2: ");
double grade11 = keyboard.nextInt();
System.out.printf("Please enter the grade of the Midterm: ");
double grade2 = keyboard.nextInt();
System.out.printf("Please enter the grade of the Final: ");
double grade3 = keyboard.nextInt();
public static double quizgrade(double grade1, double grade11) {
//quiz
double a = grade1
double b = grade11
double c = (a + b);
double d = (c / 20);
double quizGrade = (d * 25);
return quizGrade;
}
public static double midtermgrade(double grade2) {
//midterm
double aa = grade2
double bb = (aa / 100);
double midtermGrade = (bb * 25);
return midtermGrade;
}
public static double finalgrade(double grade3) {
//final
double aaa = grade3
double bbb = (aaa / 100);
double finalGrade = (bbb * 50);
return finalGrade;
}
double overallGrade = (quizgrade + midtermgrade + finalgrade);
System.out.printf("Quiz 1 grade is: %f\n", grade1);
System.out.printf("Quiz 2 grade is: %f\n", grade11);
System.out.printf("Midterm grade is: %f\n", grade2);
System.out.printf("Final grade is: %f\n", grade3);
//figuring letter grade
if (overallGrade >= 90) {
System.out.printf("Your overall grade is: %f\n", overallGrade);
System.out.printf("Your letter grade is: A\n");
}
if (overallGrade >= 80 && overallGrade <= 89) {
System.out.printf("Your overall grade is: %f\n", overallGrade);
System.out.printf("Your letter grade is: B\n");
}
if (overallGrade >= 70 && overallGrade<= 79) {
System.out.printf("Your overall grade is: %f\n", overallGrade);
System.out.printf("Your letter grade is: C\n");
}
if (overallGrade >= 60 && overallGrade<= 69) {
System.out.printf("Your overall grade is: %f\n", overallGrade);
System.out.printf("Your letter grade is: D\n");
}
if (overallGrade < 60) {
System.out.printf("Your overall grade is: %f\n", overallGrade);
System.out.printf("Your letter grade is: F\n");
}
}
有任何帮助吗?这种材料令人困惑,永无止境!
答案 0 :(得分:1)
您的第一个目标应该是让代码执行以便您可以检查它并了解正在发生的事情,然后您可以了解OOP的其他方面。
在你的第一段代码中,你已经围绕过程代码包装了一个类,因为它在你的静态main方法中,它将在你运行程序时执行。 但是,在第二次尝试中,您不会在任何地方创建类(对象)的实例,那么如何执行任何操作?没有主要方法,因此除非您在其他地方创建grade2实例,否则您的代码将永远不会执行。
对于具有单个类的实现,您将在grade2对象中使用静态main方法创建自身的实例(我建议使用大写名称的类名称),如下所示:
public class Grade2 {
static void main(String[] args) {
Grade2 grade2 = new Grade2(); // Construct (instantiate) the object.
// Access methods etc. on the grade2 instance.
grade2.doStuff(args);
}
// Other methods etc.
}
因此,通过创建类的实例,您可以使用实例(grade2)来调用方法和访问变量等。我不想让您过载(公共,私有,受保护)。但是,如果你可以围绕这个,那么你将开始在OOP的其他方面获得牵引力。
祝你好运!答案 1 :(得分:0)
,它是该类的成员。如果它不是静态的,那么它就是该类实例的成员。还要研究构造函数,这是在创建类的新实例时调用的第一件事。
离。
public class grade2{
static{
//this is a static block that is only called on the first time an instance of the class is ran
}
public grade2(){
//this is a constructor
}
}
答案 2 :(得分:0)
好吧......我有时候会觉得这就像骑自行车......你没有'拥有它'但是一旦你做到了,你就终生拥有它。
你想用一些类解决这个问题......好吧。
首先,将其分为两类:一个程序(将接受来自用户的输入的东西)和一个模型(或域)。
您的模型只是一系列课程。简单来说,让我们创建一个名为
的类学生 一个叫做的课 考试
学生可能会有一些字段 私有字符串名称;
也许还有其他人, 私人考试中期; 私人考试决赛; 私人考试测验;
为这些字段创建getter和setter
好的......我希望你还和我在一起。如果这看起来很简单,请尝试将考试加权实施到考试的子类中。您的考试超类(如果您选择转到子类路线)有一个名为getWeightedExamMark的方法或任何您想要调用它的方法。它返回一个double并在其签名中接受一个double。通过将Exam子类化为Final,Midterm和Quiz,可以覆盖该方法以应用规则值。
实际上,你应该将Student类的三个Exam对象移动到一个ReportCard类中...然后你在getMark()的ReportCard上有一个方法,它从三个考试子类中计算出getWeightedExamMark方法来渲染最终成绩。
我会编写代码,但是最好引导你,这样你就可以自己学习语法和结构。
祝你好运。答案 3 :(得分:0)
首先,您需要确定需要表示的真实世界对象
在您的情况下,其成绩,测试,用户
测试可以是类型 - 测验,MidTerm,最终
每位用户都将获得每种测试的等级
Class User {
List<Test> testList ;
public double getQuizGrade() { .. }
public double getMidTermGrade() { .. }
public double getFinalTermGrade() {..}
public String getOverAllGrade() { .. }
}
Class Test {
// define an enum of TestType( QUIZ , MIDTERM , FINALTERM)
Grade g ;
}
Class Grade {
double marks ;
}
现在,一旦您对这些类进行了建模,您可以继续添加一个主方法,该方法将捕获用户进行的每个测试的等级,并调用相应的用户方法计算并为您提供等级
答案 4 :(得分:0)
我建议将其迁移到https://codereview.stackexchange.com/。
基本类是实例的模型(也称为对象)。可以通过此类公开的方法使用一个类的所有实例。 例如:
class Color {
// instance variable
String name;
// constructor
Color(String nameOfColor) {
name = nameOfColor;
}
}
Color red = new Color("red");
Color green = new Color("green");
System.out.println(red.name) // prints "red"
System.out.println(green.name) // prints "green"
这就是上面发生的事情:
您可以看到我们对两种颜色使用了相同的方法(名称),但结果却不同。这是因为每个实例的变量不同,但模型(类)保持不变。
您确实使用了静态方法,它们无法访问实例的变量(状态),但可以直接在类上调用(不需要实例)。但是用静态方法编程不是面向对象的,即不是基于类的编程。它的功能编程。
答案 5 :(得分:0)
可以这样:
public class GradesReport{
//Here you store keyboard input
private double quizOneGrade;
private double quizTwoGrade;
private double midtermGrade;
private double finalGrade;
public printAllGrades(){
//Send all attributtes to standart output
}
public getOverallGrade(){
//Do your algorithm magic here
}
// Getters and setters here. If you want some formatting do that there
使用来自另一个具有main方法的类的键盘输入来实例化此类。
答案 6 :(得分:0)
只是为了咯咯笑,我的重构。没有真正完成,但设置为显示一些想法:
toString()
方法玩得开心。
import java.io.InputStream;
import java.util.Scanner;
public class Grade2 {
/** In fractions of available points. */
final double quiz1Grade;
/** In fractions of available points. */
final double quiz2Grade;
/** In fractions of available points. */
final double midtermGrade;
/** In fractions of available points. */
final double finalGrade;
Grade2(double quiz1Grade, double quiz2Grade, double midtermGrade,
double finalGrade)
{
this.quiz1Grade = quiz1Grade;
this.quiz2Grade = quiz2Grade;
this.midtermGrade = midtermGrade;
this.finalGrade = finalGrade;
}
/** returns a final grade in percentage points. */
public double overallGrade()
{
// weighted sum out of 100
return ((quiz1Grade + quiz2Grade) * 25) +
(midtermGrade * 25) +
(finalGrade * 50);
}
private static String letterGrade(double overallGrade)
{
final String letter;
//figuring letter grade
if (overallGrade >= 90) {
letter= "A";
} else if (overallGrade >= 80 && overallGrade <= 89) {
letter= "B";
} else if (overallGrade >= 70 && overallGrade<= 79) {
letter= "C";
} else if (overallGrade >= 60 && overallGrade<= 69) {
letter= "D";
} else if (overallGrade < 60) {
letter= "F";
} else {
letter = "UNKNOWN "+overallGrade;
}
return letter;
}
public void reportGrade()
{
double overallGrade = overallGrade();
String letter = letterGrade(overallGrade);
System.out.printf("Your overall grade is: %f\n", overallGrade);
System.out.printf("Your letter grade is:" + letter + "\n");
}
@Override
public String toString()
{
return String.format("Score for Quiz 1 is: %f\n", quiz1Grade) +
String.format("Score for Quiz 2 is: %f\n", quiz2Grade) +
String.format("Score for Midterm is: %f\n", midtermGrade) +
String.format("Score for Final is: %f\n", finalGrade);
}
public static Grade2 readGradeFrom(final InputStream in)
{
Scanner keyboard = new Scanner(in);
System.out.printf("Please enter the grade of Quiz 1: ");
int quiz1 = keyboard.nextInt();
// in points out of 10
System.out.printf("Please enter the grade of Quiz 2: ");
int quiz2 = keyboard.nextInt();
// in points out of 10
//midterm method
System.out.printf("Please enter the grade of the Midterm: ");
int midterm = keyboard.nextInt();
// in points out of 25
//final method
System.out.printf("Please Enter the grade of the Final: ");
int finalTest = keyboard.nextInt();
// in points out of 100
return new Grade2( quiz1 / 10.0, quiz2/10.0, midterm / 25.0, finalTest / 100.0);
}
public static void main(String args[])
{
Grade2 grade = readGradeFrom(System.in);
grade.reportGrade();
}
}