Question
can i get an example of a java code using a class and user defined menthod with everything else listed in the picture
20% Control structures decision: if. else if.... Else switch ... case (optional) repetition (at least 2 iterations) while, do
0 0
Add a comment Improve this question Transcribed image text
Answer #1

SOURCE CODE:

*Please follow the comments to better understand the code.

**Please look at the Screenshot below and use this code to copy-paste.

***The code in the below screenshot is neatly indented for better understanding.

This is a very big question.
I have solved all of them.
Please give me an upvote dear, Much Appreciated.!!

MidTermProject.java

import java.util.Scanner;

public class MidtermProject {
    public static void main(String[] args) {
        // declare the scanner to read data from user
        Scanner in = new Scanner(System.in);
        // initialise a new Library
        Library track = new Library();
        // Initialise a media item
        MediaItem obj = new MediaItem();
        int choice = 0;
        // Loop here (WHILE LOOP)
        while (choice != 5){
            // diaplay the menu
            choice = track.displayMenu();
            // SWITCH CASE HERE 
            switch(choice){
                // if the user enters 1
                case 1: System.out.print("What is the title you are entering? ");
                    obj.title = in.nextLine();
                    System.out.print("\n To enter the format correctly; for movies, use DVD, VHS or Blue-Ray. For games, use the platform (Windows, Mac, XBox, etc.) the game runs on.");
                    System.out.print("\nWhat is the format? ");
                    obj.format = in.nextLine();
                    track.addNewItem(obj.title, obj.format);
                    System.out.print("\n");
                    break;
                // if the user enters 2
                case 2: System.out.print("Which title are you loaning? ");
                    obj.title = in.nextLine();
                    System.out.print("Who are you loaning it to? ");
                    obj.loanedTo = in.nextLine();
                    System.out.print("When did you loan the item? ");
                    obj.dateLoaned = in.nextLine();
                    track.markItemOnLoan(obj.title, obj.loanedTo, obj.dateLoaned);
                    break;
                // if the user enters 3
                case 3: track.listAllItems();
                    break;
                // if the user enters 4
                case 4: System.out.println("Which title are you returning? ");
                    obj.title = in.nextLine();
                    track.markItemReturned(obj.title);
                    break;
                // if the user enters 5, quit
                case 5: System.out.println("Goodbye");
                    break;
                default:
                    // if the user enters other than 1 to 5, invalis
                    System.out.println("Invalid option. Please select from 1 - 5.");
                    System.out.println("\n");
            }
        }
        // close the reader
        in.close();
    }

}

========

Library.java

=====\

import java.util.Scanner;
public class Library {
    // fields 
    static Scanner in = new Scanner(System.in);
    MediaItem t = new MediaItem();
    MediaItem[] items = new MediaItem[100];
    String[] str = new String[100];
    int numberOfItems = 0; //fields
    int check = 0;
    int called = 0;
    
    // menu
    int displayMenu(){ //methods
        System.out.println("1. Add new item \n2. Mark an item as on loan \n3. List all items \n4. Mark an item as returned \n5. Quit");
        System.out.print("\nWhat would you like to do? ");
        int a = in.nextInt();
        System.out.println ("\n");
        return a;
    }
    // add an item to library
    void addNewItem(String title, String format){
        MediaItem item = new MediaItem(title, format);
        items[numberOfItems] = item;
        numberOfItems++;
    }

    // mark item on loan
    void markItemOnLoan(String title, String name, String date){
        for(int b = 0; b < numberOfItems; b++){
            if(title.equals(items[b].title)){
                items[b].markOnLoan(name, date);
                called = 1;
            }
        }
        if(called == 0)
            System.out.println(title + " is not part of your existing Library. \n");
        called = 0;
    }
    
    // list all items
    void listAllItems(){
        for(int c = 0; c < numberOfItems; c++){
            if (items[c].onLoan)
                str[c] = "\n" + items[c].title + " " + items[c].format + " loaned to " + items[c].loanedTo + " on " + items[c].dateLoaned;
            else
                str[c] = "\n" + items[c].title + " " + items[c].format;
            System.out.println(str[c] + "\n");
        }
    }
    // mark as returned
    void markItemReturned(String title){
        for(int b = 0; b < numberOfItems; b++){
            if(title.equals(items[b].title)){
                items[b].markReturned();
                check = 1;
            }
        }
        if(check == 0)
            System.out.println("Sorry, I couldn't find " + title + " in the library.");
        check = 0;
    }

}

=========

MediaItem.java

=======

public class MediaItem {
    // fields
    String title;
    String format;
    boolean onLoan;
    String loanedTo;
    String dateLoaned;

    MediaItem(){ //default constructor
        title = null;
        format = null;
        onLoan = false;
        loanedTo = null;
        dateLoaned = null;
    }
    MediaItem(String title, String format){ //constructor
        onLoan = false;
        this.title = title;
        this.format = format;
    }

    //getters and setters are here
    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getFormat() {
        return format;
    }

    public void setFormat(String format) {
        this.format = format;
    }

    public boolean isOnLoan() {
        return onLoan;
    }

    public void setOnLoan(boolean onLoan) {
        this.onLoan = onLoan;
    }

    public String getLoanedTo() {
        return loanedTo;
    }

    public void setLoanedTo(String loanedTo) {
        this.loanedTo = loanedTo;
    }

    public String getDateLoaned() {
        return dateLoaned;
    }

    public void setDateLoaned(String dateLoaned) {
        this.dateLoaned = dateLoaned;
    }

    // mark on loan
    void markOnLoan(String name, String date){ //methods
        if(onLoan)
            System.out.println(this.title + " is already loaned out");
        else {
            onLoan = true;
            loanedTo = name;
            dateLoaned = date;
        }
    }
    // mark returned
    void markReturned(){
        if(!onLoan)
            System.out.println(this.title + " is not currently loaned out");
        onLoan = false;
    }
}

=======

CODE SCREENSHOT:

ها 37 | 38 39 public boolean isOnLoan() { return on Loan; 40 41 O نها 42 43 o public void setOnLoan(boolean onLoan) { this.on6 © Medialtem.java x C Midterm Project.java x © Library.java 1 import java.util.Scanner; 2 public class Library { 3 // fields

OUTPUT SCREENSHOT:

1. Add new item 2. Mark an item as on loan 3. List all items 4. Mark an item as returned 5. Quit What would you like to do? 2

If there are any doubts, comment down here. We are right here to help you. Happy Learning..!! PLEASE give an UPVOTE I Have Pu

Add a comment
Know the answer?
Add Answer to:
can i get an example of a java code using a class and user defined menthod...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • can i get an example of a java code using a class and bulit in methods...

    can i get an example of a java code using a class and bulit in methods inculding everything else in the picture 20% Control structures decision: if. else if.... Else switch ... case (optional) repetition (at least 2 iterations) while, do...while for 20% Classes (at least 2 classes) 10% Methods (at least 2 methods) user-defined and/or built-in methods 10% Arrays (at least 1 set of array) 20% User Interface Menu like starting point User friendliness Error free

  • ***Please give the java code for the below and add comments for different areas for a...

    ***Please give the java code for the below and add comments for different areas for a dummy to understand*** This java program will combine the techniques of handling arrays, decision control statements, and loops. Your program should accomplish the following: Build an array that holds characters (size 35) Load the array with random capital letters (use random generator) Your program should present a menu to the user allowing for the following “actions”. To “search” for a target letter of the...

  • Q1. Write a program in Java         a. Create 2 separate arrays, of user defined values,...

    Q1. Write a program in Java         a. Create 2 separate arrays, of user defined values, of same size         b. Add these 2 arrays. ........................................................................................................................... Q2. Write a program in java (using loop) input any10 numbers from user and store in an array. Check whether each of array element is positive, negative, zero, odd or even number. ............................................................................................................................ Q3. Write a program in Java to display first 10 natural numbers. Find the sum of only odd numbers. .............................................................................................................................. Q4....

  • Homework 3: Input Validation 1   Objectives control structures console-based user input using Scanner class writing complete...

    Homework 3: Input Validation 1   Objectives control structures console-based user input using Scanner class writing complete programs using two classes: client and supplier 2   User Interface Specification This is a console-based I/O program. Display should go to System.out (print or println) and the program will get user input using the Scanner class. The flow of execution should be as follows: When the program starts, display a one-line introduction to the user Display a menu with 5 options 1. validate zip...

  • User interfaces are a critical part of any system. In this lesson, a Graphical User Interface...

    User interfaces are a critical part of any system. In this lesson, a Graphical User Interface (GUI) is created. The code generated will link the components to the action. Assignment: Create a Graphical User Interface that has two buttons and a textArea (See Examples 12.3 and Example 12.4). figure 1 Create a String array to store the following messages (Enter your name where it says YourName). "Congratulations YourName!nYou completed the Java class. nYou learned to write Java programs with commonly...

  • can i get an example of a simple code using the following picture in java 20%...

    can i get an example of a simple code using the following picture in java 20% Basic Java usage of comments next to the program variables declaration: input/output data types

  • Please help me code the following in: JAVA Please create the code in as basic way...

    Please help me code the following in: JAVA Please create the code in as basic way as possible, so I can understand it better :) Full points will be awarded, thanks in advance! Program Description Write a program that demonstrates the skills we've learned throughout this quarter. This type of project offers only a few guidelines, allowing you to invest as much time and polish as you want, as long as you meet the program requirements described below. You can...

  • Note: According to the question, please write source code in java only using the class method....

    Note: According to the question, please write source code in java only using the class method. Sample Run (output) should be the same as displayed in the question below. Make sure the source code is working properly and no errors. Exercise #2: Design and implement a program (name it compareArrays) that compares the content of 2 single-dimensional arrays of the same size. The program prompts the users to enter the array size. Then prompts the user to initialize each array...

  • Need assistance with this problem. This is for a java class and using eclipse. For this...

    Need assistance with this problem. This is for a java class and using eclipse. For this assignment we’ll be doing problems 21.3 and 21.4 from your textbook. Problem 21.3 asks you to write a generic method that removes duplicate items from an ArrayList. This should be fairly straightforward. Problem 21.4 asks you to implement insertion sort within a generic method. Insertion sort is described in detail on pages 250-252 of your textbook. You can write your two methods in a...

  • I need code in java The Student class: CODE IN JAVA: Student.java file: public class Student...

    I need code in java The Student class: CODE IN JAVA: Student.java file: public class Student {    private String name;    private double gpa;    private int idNumber;    public Student() {        this.name = "";        this.gpa = 0;        this.idNumber = 0;    }    public Student(String name, double gpa, int idNumber) {        this.name = name;        this.gpa = gpa;        this.idNumber = idNumber;    }    public Student(Student s)...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT