• ベストアンサー

Javaのエラーの箇所

(絶っっ対、無理)。 そんな無茶な教授から宿題が出ました。 しかも大急ぎです(後二時間しかない…)。 どうかどこにどんなエラーが出るのか教えて下さい。 For the declaration of I1: interface I1{ int I1f1(); void I1f2(int i); } identify the errors. (a) in the declaration of the interface I2: interface I2 extends I1{ double I2f1(); void I2f2(); int I1f1(); double I2f1(){return 10;} private int AC1f4(); private int n=10; } (b) in the declaration of class CI1: class CI1 implements I1{ int I1f1(){.....} void I1f2(int i){.....} int CI1f3(){.....} } (c) and in the declaration of object c6: I1 c6 = new I1(); 勘ですが、(a)はinterface I1がpublicと宣言されてないので (a)の中の int I1f1(); が呼べないのでしょうか? (b)と(c)はまったく分かりません。

  • ginkgo
  • お礼率94% (132/139)
  • Java
  • 回答数2
  • ありがとう数2

質問者が選んだベストアンサー

  • ベストアンサー
  • kero-pi
  • ベストアンサー率66% (2/3)
回答No.2

(a)はインターフェースのメソッドに処理を実装する事はできない。 (b)はインターフェースのメソッドを実装する時は、public 修飾子が必須。 (c)はインターフェースをnewする事はできない。 って回答で良いですか?

ginkgo
質問者

お礼

もう提出してしまいました。 (a)はなんとか正解でした。 (b)はしくじったようです。 (c)もですね…。 この授業、おかしいです。 教授に抗議します。 ご回答ありがとうございました。

その他の回答 (1)

  • uratan
  • ベストアンサー率30% (38/124)
回答No.1

バグを見つけるには、まずエラーメッセージを見て大体の見当をつけるものです。 なので、エラーメッセージを書いてくれると、回答しやすいのですが・・・。

ginkgo
質問者

お礼

質問の内容の最初の部分が切れてしまいました… 実は私はまだ"Hello world!"さえも表示できないほどのJava初心者です(ついさっきやっと出来ましたけど)。 たった二時間の講義だけでJavaのプログラムのエラーを見つけろ、って…無理ですよね、絶対? この質問ではそのエラーメッセージが知りたかったのです。 ご回答ありがとうございました。

関連するQ&A

  • Javaについて

    Javaについての質問です。 下記のプログラムは Abst_Class show1 can be inheritted to a subclass num1=1 Class1 show2 is created in Class1 num1=1 num2=2 Class2 show1 is inheritted and overridden in Class2 num1=10 str=SSSSSS Class2 show2 is created in Class2 num1=10 str=SSSSSS と出力するプログラムなのですがエラーが出て出力できません。どこがいけないのか教えていただけないでしょうか? お願いいたします。 interface itr{ itr(){} void show(); void dai(); } abstract abr implements itr{ abr(){ int num1; num1=1; } public void dai(){ if(num1==0){ num1=1; } } public void show(){ dai(); System.out.println("Abst_Class show1 can be inheritted to a subclass num1="+num1); } abstract show1(); } class Class1 extends abr{ int num2; Class1(int a,int b){ num1=a; num2=b; } public void show1(){ super.show(); } void show2(){ super.show(); System.out.println("Class1 show2 is created in Class1 num1="+num1+"num2="+num2); } } class Class2 extends abr{ String str; Class2(int num,String str){ this.str=str; num1=num; } public void show1(){ System.out.println("Class2 show1 is inheritted and overridden in Class2 num1="+num1+"str="+str); } void show2(){ System.out.println("Class2 show1 is inheritted and overridden in Class2 num1="+num+"str="+str); } } class K10_6{ public static void main(String args[]){ Class1 f1 = new Class1(1, 2); Class2 f2 = new Class2(10,"SSSSSS"); f1.show1(); f1.show2(); f2.show1(); f2.show2(); } } .\abr.java:1: class、interface、または enum がありません。 abstract abr implements itr{ ^ .\abr.java:4: class、interface、または enum がありません。 num1=1; ^ .\abr.java:5: class、interface、または enum がありません。 } ^ .\abr.java:6: class、interface、または enum がありません。 public void dai(){ ^ .\abr.java:9: class、interface、または enum がありません。 } ^ .\abr.java:11: class、interface、または enum がありません。 public void show(){ ^ .\abr.java:13: class、interface、または enum がありません。 System.out.println("Abst_Class show1 can be inheritted to a subclass num1="+num1 ); ^ .\abr.java:14: class、interface、または enum がありません。 } ^ .\abr.java:15: class、interface、または enum がありません。 abstract show1(); ^ .\abr.java:16: class、interface、または enum がありません。 }

    • ベストアンサー
    • Java
  • C++ proxy class の質問

    C++の学習者です。Visual Studio Community 2015の上で、教本を使って勉強しています。 その中でproxy class のサンプルコードがあり、忠実にキーボードから入力してビルドしようとしたのですが、添付画面写真のようなエラーメッセージが出て、出来ませんでした。 ちゃんとクラスの定義ファイルもありますので、「識別子がクラス名でも名前空間名でもありません。」などというメッセージがどうして出るのかわかりません。 詳しい方がいらっしゃいましたら、どうぞ教えて頂きたく、お願いいたします。 プロジェクトに含まれるソースファイルやヘッダーファイルなどを下にコピーしてあります。 (1) メインプロジェクトファイル : // ConsoleApplication84.cpp : コンソール アプリケーションのエントリ ポイントを定義します。 // example of proxy class #include "stdafx.h" #include <iostream> #include <iomanip> #include <cstdlib> #include <ctime> #include <cmath> #include <cstring> #include <new> using namespace std; #include "interface.h" int main() { Interface i(5); cout << "interface contains : " << i.getValue() << " before setValue()" << endl; i.setValue(10); cout << "interface contains : " << i.getValue() << " after setValue()" << endl; return 0; } (2) Implementation クラスのヘッダー... クライアントから隠しておきたいクラス #pragma once // header file for class Implementation // example of proxy class class Implementation { public: // constructor Implementation(int v) : value(v) // initialization syntax { // empty body } // set value to v void setValue(int v) { value = v; } // return value int getValue() const { return value; } private: int value; };// end class definition (3) Interface クラスのヘッダー ... Implementation の proxy class #pragma once // header file for Interface class class Implementation; // forward class declaration // use this format when a pointer or reference to // class Implementation is used // do not write as " #include "inplementation.h" " class Interface { // this is the proxy class of Implementation class public: Interface(int); void setValue(int); int getValue() const; ~Interface(); private: Implementation *ptr; // use a pointer to an object in Implementation class }; 【4】Interface クラスの関数定義 // interface.cpp // definition of member function for Interface class #include "interface.h" #include "implementation.h" #include "stdafx.h" // constructor Interface::Interface(int v) : ptr (new Implementation(v)) // initialize pointer { // empty body } // set value function void Interface::setValue(int v) { ptr->setValue(v); // do not take the form of assigning the value to the private pointer ptr // but use the public function of setValue() of Implementation class through pointer ptr // this way the client(or main() program ) of class Implementation does not access to // the actual inside code of the class definition } // return value int Interface::getValue() const { return ptr->getValue(); } // destructor Interface::~Interface() { delete ptr; }

  • java コンパイルエラー (初心者です)

    次のようなソースコードを入力してコンパイルすると、 class, interface, or enum expectedというエラーが出ます。 なぜでしょうか?{}の数はあってると思いますが... dentaku.java class dentaku{ public static void main (String args[]){ //3つの変数「a」と「b」と「c」を宣言 int a =0; int b =1; int c =1; System.out.println("xを10進数,aを2進数の1の位、bを10の位、cを100の位とするとと" + (x=a^2+b^2+c^2) + "です。"); } }

    • ベストアンサー
    • Java
  • Javaコンパイルエラー

    Javaにて以下のようなプログラムを作成していますが、 「演算子 ^ は引数の型 double, int で未定義です。」とエラーになりました。 色々な方のご協力もあり、ここまで作れましたが、 これより先が行き詰っています。 教えていただけないでしょうか。 ---------------------------------------------- // 分散を返すメソッド public class Kadai16 { public static void main(String args[]){ //double型の配列の生成 double[]a=new double[10]; argument(a); //配列aの要素[0]から[9]の内容を表示 System.out.println(a[0]); System.out.println(a[1]); System.out.println(a[2]); System.out.println(a[3]); System.out.println(a[4]); System.out.println(a[5]); System.out.println(a[6]); System.out.println(a[7]); System.out.println(a[8]); System.out.println(a[9]); //double total=sum(a)を実行する double total =sum(a); System.out.println("合計は"+total+"です。"); //使って平均を出す double heikin; mean(total); heikin = mean(total); System.out.println("平均は"+heikin+"です。"); //分散を返す double bunsan; bunsan = total-heikin^2/10; System.out.println("分散は"+bunsan+"です。"); } /** * ランダムな数値を入れるメソッド * @param a ランダムな数値を入れる配列 */ private static void argument(double[]a) { //引数の配列aの[0]から[9]までランダムな数値を設定する。 for(int i=0; i<a.length;i++) { a[i]=Math.floor(Math.random()*10); } } //配列を受け取って要素の合計を出すメソッド public static double sum(double[] x) { double t=0; for(int i=0; i<x.length;i++) { t= x[i]+t; } return t; } //配列を受け取って平均を出すメソッド public static double mean(double b) { return b/10; } //分散を返すメソッド public static double bunsan(double c) { double t=0; double bnsan =0; for(int i=0; i<c.length; i++) { t =c[i]+t } } }

  • Javaのスレッドに関して質問です

    Assistantクラスを使い待機状態と再開を確認できるプログラムの作成 loafとrestartメソッドを設ける workメソッドが呼ばれる度loafを呼び出す Managerクラスを定義 checkメソッドを設け、Assistantをcheckし続ける loaf状態ならrestartさせる (Managerクラスはデーモンスレッド) ということなんですが、いまいちうまくいきません さぼっても復帰してくれません public class Assistant implements Runnable { private String name; private Chore c; public Assistant(String name, Chore c) { this.name = name; this.c = c; } public void run() { work(); } public void work() { while (true) { synchronized (c) { if (c.doEnd()) break; System.out.println(name + " : " + c.digest()); loaf(); } } } public synchronized void loaf() { try { c.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } public synchronized void restart() { c.notify(); } } public class Chore { private String name; private int step; private int id; public Chore(String name) { this.name = name; this.step = this.name.length(); this.id = 0; } public synchronized String digest() { String message = "" + id + name.charAt(id); try { Thread.sleep(500); } catch (InterruptedException ie) { } id++; return message; } public synchronized boolean doEnd() { return id >= step; } } public class Manager extends Thread { private String name; private Assistant a; public Manager(String name) { this.name = name; } public void run() { check(); } public void check() { a.restart(); } } public class Test { public static void main(String[] args) { Chore[] ch = { new Chore("掃除"), new Chore("プリント印刷"), new Chore("出欠データ入力") }; Assistant[] a = { new Assistant("あ", ch[0]), new Assistant("\tい", ch[1]), new Assistant("\t\tう", ch[2] }; Thread[] t = new Thread[a.length]; for (int i = 0; i < t.length; i++) { t[i] = new Thread(a[i]); } for (int i = 0; i < t.length; i++) { t[i].start(); } Manager m = new Manager("監査"); m.setDaemon(true); m.start(); for (int i = 0; i < t.length; i++) { try { t[i].join(); } catch( InterruptedException ie ) { } } }

    • ベストアンサー
    • Java
  • javaのことで

    ---MainPanel.java--- import java.awt.Dimension; import java.awt.Graphics; import java.event.MouseEvent; import java.event.MouseListener; import javax.swing.JPanel; import java.util.Random; public class MainPanel extends JPanel implements MouseListener { public static final int WIDTH = 640; public static final int HEIGHT = 480; private static final int NUM_FILE = 4; private File[] file; private int prev; public MainPanel() { setPreferredSize(new Dimension(WIDTH, HEIGHT)); file = new File[NUM_FILE]; for (int i = 0; i < NUM_FILE; i++) { file[i] = new File(i, this); } select(); addMouseListener(this); } private void clear() { for (int i = 0; i < NUM_FILE; i++) { file[i].delete(); } } private boolean check(int a, int b) { return a == b || a == prev || b == prev; } private void select() { Random rand = new Random(); int a, b; do { a = rand.nextInt(NUM_FILE); b = rand.nextInt(NUM_FILE); } while (check(a, b)); file[a].set(0, 0); file[b].set(file[a].getX() + file[a].getWith() + File.SPACE, 0); } public void paintComponent(Graphics g) { super.paintComponent(g); for (int i = 0; i < NUM_FILE; i++) { if (file[i].isSelected()) { file[i].draw(g); } g.drawString("ファイル" + (i + 1) + " … " + file[i].getCnt(), 0, HEIGHT * 3 / 4 + i * 15); } } public void mouseClicked(MouseEvent e) { int x = e.getX(); int y = e.getY(); for (int i = 0; i < NUM_FILE; i++) { if (file[i].isSelected()) { // 表示されていて // 画像内なら if (x > file[i].getX() && x < file[i].getX() + file[i].getWidth() && y < file[i].getY() && y > file[i].getY() + file[i].getHeight()) { file[i].count(); prev = i; clear(); select(); break; } } } repaint(); } public void mousePressed(MouseEvent e) { } public void mouseReleased(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } } フレームを用意してMainPanelを付加し,pack();してください.

  • javaの問題

    import java.util.Scanner; class test6{ static void printArray(int[] a) { for(int i=0;i<a.length;i++) System.out.print(String.format("%1$6s", (a[i]+" "))); System.out.println(); } static void printArray(int[][] a) { for(int[] i:a) { for(int j:i) System.out.print(String.format("%1$6s",(j+"\t"))); System.out.println(); } } public static void main(String[] args) { int[] a = {12,536,-8,7}; int[][] c = {{32,-1,32,45,67}, {535,99999,2}, {2,5,-123,9}}; printArray(a); printArray(c); } } このプログラムを表示するとこのようになるのですが、 12 536 -8 7 32 -1 32 45 67 535 99999 2 2 5 -123 9 これに一工夫加えて見やすくしたいです。 このような形にするのはどのようにすればよいのでしょうか + + | 32 -1 45 67 | | 535 99999 2 | | 2 5 -123 9 | + +

  • javaのインターフェースと動的制御について教えてください!

    javaのインタフェースと動的制御についての課題を出されたのですが、以下のようなものです ・起動させるクラス名はmain実行時にコマンドライン引数として渡す ・動的に呼び出されるクラスは2つ作成し、クラスその(1)現在の時間、 クラスその(2)クラス名を出力させる。 ・想定している構成として、メインクラス、インターフェース、動的ク ラス二つの計4クラスとする。 という問題で今のところ作成したクラスが以下のようなものです、インターフェースクラスと動的クラス二つはなんとかできて、このまま変化させずに置きたいです。メインクラスの見当がつきません、できたらメインクラスのアドバイスをどうかお願いします。ちなみに私はエクリプスをしようしています。 インターフェースクラス package Kadai1221; public interface Interface001 { void kurasu(); } 動的クラス二つ 一つめ package Kadai1221; import java.util.Calendar; public class Jikan implements Interface001 { public void kurasu() { Calendar now = Calendar.getInstance(); int zikan; int hun; int byou; zikan = now.get(Calendar.HOUR_OF_DAY); hun = now.get(Calendar.MINUTE); byou = now.get(Calendar.SECOND); System.out.println(zikan + "時" + hun + "分"          + byou + "秒"); } } 二つ目 package Kadai1221; public class Kurasumei implements Interface001 { public void kurasu() { System.out.println("Kurasumei"); } } メインクラス package Kadai1221; public class Kadaimain { /** * @param args */ public static void main(String[] args) { // TODO 自動生成されたメソッド・スタブ } } どうかよろしくお願いします。

  • 自作の行列クラスを継承するさいにエラーがでます

    現在、c++の学習で、自作の基底行列クラスMatrixを作成し、このクラスを継承して新たにlduMatrixを作成する事を考えています。 が、継承し、 lduMatrix a( 3, 3); a = 3; とすると、 main.C:13: warning: passing ‘double’ for argument 1 to ‘lduMatrix::lduMatrix(int)’ というエラーがでてコンパイルできずにいます。その一方で、 lduMatrix a(3,3, 3.14); とするとコンパイルはとおり、lduMatrix行列の各要素(a[1][1]など)の値をプリントさせると、[[3.14]]の値が入っていることを確認しております。 どこが間違っているのか御指導いただけると幸いです。 以下、クラスの中身です。よろしくおねがいします。 《Class: Matrix》 #include <iostream> class Matrix{ private: //! Size of row and column in Matrix int row_, col_; //! Row pointers double** m_; //! Allocate function for row-pointers void allocate(); public: Matrix(); //! Constructor with given matrix size Matrix( const int, const int ); //! Constructor with given matrix size and value fro all elements Matrix( const int, const int, const double ); //! Destructor ~Matrix(); ・・・省略・・・ double* operator[]( const int ); double* operator[]( const int ) const ; void operator=( const double ); }; /* Private functions *********************************************** */ void Matrix::allocate() { m_ = new double* [row_]; m_[0] = new double [row_*col_]; for ( int i=1; i<row_; i++ ){ m_[i] = m_[i-1] + col_; } } /* Destructor ****************************************************** */ Matrix::~Matrix(){ delete[] m_[0]; delete[] m_; } /* Constructors **************************************************** */ // NULL constructor Matrix::Matrix() : row_(0), col_(0), m_(NULL) {} // Constructor with given matrix size Matrix::Matrix( const int row, const int col ) : row_(row), col_(col) { allocate(); } // Constructor with given matrix size and value for all elements Matrix::Matrix( const int row, const int col, const double s ): row_(row), col_(col) { allocate(); double* m = m_[0]; for ( int i=0; i<row_*col_; i++ ){ m[i] = s; } } 《省略》 /* Member operators ************************************************ */ double* Matrix::operator[]( const int i ){ return m_[i]; } void Matrix::operator=( const double t ){ double* m = m_[0]; int nm = row_*col_; for ( int i=0; i<nm; i++ ) { m[i] = t; } } 《Class: lduMatrix》 class lduMatrix : public Matrix{ public: lduMatrix(); lduMatrix( const int ); lduMatrix( const int, const double ); }; lduMatrix::lduMatrix() {} lduMatrix::lduMatrix( const int mSize ) : Matrix( mSize, mSize, 0.0 ) {} lduMatrix::lduMatrix( const int mSize, const double s ) : Matrix( mSize, mSize, s ) {}

  • javaのprivateって?

    privateの必要性がいまいちわからなくて困ってます。 class Car{ private int num; private double gas; public void setNumGas(int n, double g){ num=n; gas=g; ・・・・・・・ このようなプログラムがあります。 変数をprivateにして、下のメソッドでnの値をnumに格納してします。 ここで変数をprivateにする意味ってあるんですか? 本には、勝手な値(-10など)を代入させないためと書いてあります。 これっておかしくないですか? 変数をprivateにしてもメソッドに「-10」を引数として渡したら余裕で格納できるじゃないですか。 代入させないためと書いてあるのに格納できるって・・・・矛盾してるような気がします。 ここでprivateにする意味がわかりません。 誰か教えてください。

専門家に質問してみよう