Problems Flashcards

1
Q

Text I/O Read and Write File a File Example

A

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

public class TextFileIOExample {

public static void main(String[] args) {

    String fileName = "textfile.txt";

    try (BufferedWriter writer = new BufferedWriter(new FileWriter(fileName))) {

        writer.write("Hello, this is a text file example.\n");

        writer.write("Writing some text to demonstrate text file I/O.\n");

    } catch (IOException e) {

        e.printStackTrace();

    }

    try (BufferedReader reader = new BufferedReader(new FileReader(fileName))) {

        String line;

        while ((line = reader.readLine()) != null) {

            System.out.println(line);

        }

    } catch (IOException e) {

        e.printStackTrace();

    }

}

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Binary I/O Read and Write a File Example

A

import java.io.*;

public class BinaryFileIOExample {

public static void main(String[] args) {

    String fileName = "binaryfile.dat";

    try (DataOutputStream dos = new DataOutputStream(new FileOutputStream(fileName))) {

        dos.writeInt(42);

        dos.writeDouble(3.14);

        dos.writeUTF("Binary file example.");

    } catch (IOException e) {

        e.printStackTrace();

    }

    try (DataInputStream dis = new DataInputStream(new FileInputStream(fileName))) {

        int intValue = dis.readInt();

        double doubleValue = dis.readDouble();

        String stringValue = dis.readUTF();

        System.out.println("Int value: " + intValue);

        System.out.println("Double value: " + doubleValue);

        System.out.println("String value: " + stringValue);

    } catch (IOException e) {

        e.printStackTrace();

    }

}

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Read input from the console and write it to a file using Scanner and FileWriter

A

import java.io.FileWriter;

import java.io.IOException;

import java.util.Scanner;

public class ConsoleInputFileOutput {

public static void main(String[] args) {

    Scanner scanner = new Scanner(System.in);

    FileWriter fileWriter = null;

    try {

        System.out.print("Enter the file name: ");

        String fileName = scanner.nextLine();

        fileWriter = new FileWriter(fileName);

        System.out.println("Enter text (type 'exit' to stop):");

        String line;

        while (!(line = scanner.nextLine()).equalsIgnoreCase("exit")) {

            fileWriter.write(line + "\n");

        }

        System.out.println("Data has been written to the file.");

    } catch (IOException e) {

        e.printStackTrace();

    } finally {

        try {

            if (fileWriter != null) {

                fileWriter.close();

            }

        } catch (IOException e) {

            e.printStackTrace();

        }

        // Close the scanner

        scanner.close();

    }

}

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Fully Loaded Constructor

A

public class Person {

private String name;

private int age;

public Person(String name, int age) {

    this.name = name;

    this.age = age;

}

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Mutator Method

A

public void setAge(int age) {

this.age = age;

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Copy Constructor

A

public class Person {

private String name;

private int age;

public Person(String name, int age) {

    this.name = name;

    this.age = age;

}

public Person(Person other) {

    this.name = other.name;

    this.age = other.age;

}

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

toString Method

A

@Override

public String toString() {

return "Person[name=" + name + ", age=" + age + "]";

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Accessor

A

public int getAge() {

return age;

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Main Method

A

public class Main {

public static void main(String[] args) {

    // Your main code here

}

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Derived Class

A

public class Student extends Person {

private int studentId;

public Student(String name, int age, int studentId) {

    super(name, age);

    this.studentId = studentId;

}

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Instance Variables

A

public class Example {

private int variable1;

private String variable2;

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Accessors and Mutators

A

public int getVariable1() {

return variable1;

}

public void setVariable1(int variable1) {

this.variable1 = variable1;

}

public String getVariable2() {

return variable2;

}

public void setVariable2(String variable2) {

this.variable2 = variable2;

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Override toString

A

@Override

public String toString() {

return "Example[variable1=" + variable1 + ", variable2=" + variable2 + "]";

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Hierarchy and Polymorphism

A

public abstract class Shape {

public abstract double calculateArea();

}

public class Circle extends Shape {

private double radius;

public Circle(double radius) {

    this.radius = radius;

}

@Override

public double calculateArea() {

    return Math.PI * radius * radius;

}

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Define and Use Recursive Method

A

public class RecursiveExample {

public int factorial(int n) {

    if (n == 0 || n == 1) {

        return 1;

    } else {

        return n * factorial(n - 1);

    }

}

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Class and Object Example

A

class Car {

}

Car myCar = new Car();

17
Q

Composition Example

A

class Component {}

class Composition {

private Component component;

}

18
Q

Inheritance Example

A

class Parent {}

class Child extends Parent {}

19
Q

Abstract Class and Interface Example

A

abstract class AbstractClass {

abstract void performAction();

}

class ConcreteClass extends AbstractClass {

void performAction() {

    System.out.println("Performing concrete action");

}

}

20
Q

Exception Handling Example

A

try {

throw new IOException("Input/Output Exception");

} catch (IOException e) {

System.out.println("Caught IOException: " + e.getMessage());

}

21
Q

GUI Basics Example

A

import javax.swing.*;

import java.awt.*;

JFrame frame = new JFrame(“GUI Example”);

JButton button = new JButton(“Click Me”);

frame.add(button);

22
Q

Recursion Example

A

static int calculateFactorial(int n) {

return n == 0 || n == 1 ? 1 : n * calculateFactorial(n - 1);

}

23
Q

File I/O Example

A

try (PrintWriter writer = new PrintWriter(“output.txt”)) {

writer.println("Hello, World!");

} catch (IOException e) {

System.out.println("Error writing to file: " + e.getMessage());

}

24
Q

Generics Example

A

class Box<T> {</T>

private T content;

public void put(T item) {

    this.content = item;

}

public T get() {

    return content;

}

}

Box<String> stringBox = new Box<>();</String>

stringBox.put(“Hello”);

String greeting = stringBox.get();

25
Q

Case Convertor

A

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class CaseConverter extends JFrame {

private JTextField inputField;
private JTextField outputField;

public CaseConverter() {
    super("Case Converter");

    inputField = new JTextField(20);
    outputField = new JTextField(20);
    outputField.setEditable(false);

    JButton convertButton = new JButton("Convert");
    convertButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            convertText();
        }
    });

    setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));

    add(new JLabel("Input Text:"));
    add(inputField);
    add(new JLabel("Converted Text:"));
    add(outputField);
    add(convertButton);

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    pack();
    setLocationRelativeTo(null);
}

private void convertText() {
    String inputText = inputField.getText();
    String convertedText = "";

    convertedText = inputText.toUpperCase();

    outputField.setText(convertedText);
}

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            new CaseConverter().setVisible(true);
        }
    });
} }